MarcusEstr
1/27/2018 - 5:31 AM

JavaScript Map Method

Map method + Quiz 22

### 22 - Quiz: I Got Bills (6-9)

var bills = [50.23, 19.12, 34.01,

    100.11, 12.15, 9.90, 29.11, 12.99,

    10.00, 99.22, 102.20, 100.10, 6.77, 2.22
];



var totals = bills.map(function(element){

    element += element * 0.15;

    return Number(element.toFixed(2));

    //toFixed rounds to two decimal places, but converts result to string.

    //Number turns strings into numbers.
 
});


console.log(totals);

map() Method

This allows you to perform an operation on elements in an array, and return a new array.

var donuts = ["jelly donut", "chocolate donut", "glazed donut"];

var improvedDonuts = donuts.map(function(donut) {
  donut += " hole";
  donut = donut.toUpperCase();
  return donut;
});

donuts array: ["jelly donut", "chocolate donut", "glazed donut"] improvedDonuts array: ["JELLY DONUT HOLE", "CHOCOLATE DONUT HOLE", "GLAZED DONUT HOLE"]

Map() method accepts one argument, a function, whcih is used to manipulate each element in the array.