Skip to content

Instantly share code, notes, and snippets.

@desinas
Last active February 19, 2021 10:23
Show Gist options
  • Save desinas/26acc3e96592df9ce4791cd35b073caa to your computer and use it in GitHub Desktop.
Save desinas/26acc3e96592df9ce4791cd35b073caa to your computer and use it in GitHub Desktop.
I got bills Quiz (6-9) of Udacity FEWD
/*
* Programming Quiz: I Got Bills (6-9)
*
* Use the .map() method to take the bills array below and create a second array
* of numbers called totals. The totals array should contains each amount from the
* bills array but with a 15% tip added. Log the totals array to the console.
*
* For example, the first two entries in the totals array would be:
*
* [57.76, 21.99, ... ]
*
* Things to note:
* - each entry in the totals array must be a number
* - each number must have an accuracy of two decimal points
*/
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(bill) {
bill = bill + (bill * 0.15);
bill = bill.toFixed( 2 );
return Number(bill);
});
console.log(totals);
@desinas
Copy link
Author

desinas commented Dec 21, 2017

What Went Well

  • Your code should have a variable bills
  • Your code should have a variable totals
  • Your bills array should call the map() method
  • Your totals array should be an array of numbers
  • Your code should print the correct totals array to the console

Feedback: Your answer passed all our tests! Awesome job!

@desinas
Copy link
Author

desinas commented Dec 21, 2017

The array is starting with a list of numbers. But when you use .toFixed() function, it will convert it into string. So, you need to use Number() function to convert it back to number.

@mahmoudelkhadragy
Copy link

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(total) {
return Number((total + total * 0.15).toFixed(2));

});
console.log(totals);

@bakhritdinov99
Copy link

bakhritdinov99 commented Feb 19, 2021

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(bill => Number((bill += 0.15*bill).toFixed(2)));
console.log(totals);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment