-1

Hello can some one help me...

we made a currency in our class and where trying to make a loop that adds the two numbers to see how much "keith bucks" we have .. thats the name of the currency " keith bucks" here the info we have

var Keith = 18;

var austin = 4.5;

var marc = 2;

var kaleb = 1.29;

var hayley = 0.9;

var michaela = 0.45;

var zacc = 0.0225;

1 Answers1

0

There's nothing to loop over here. You've defined each person as a separate variable.

Consider changing your data structure:

let people = [
  { name: 'Keith', amount: 18 },
  { name: 'austin', amount: 4.5 },
  // etc. etc.
];

From there you can use something like this:

let total = 0;
people.forEach((person) => {
  total += person.amount;
});
console.log(total);

Once you have that mastered, look into map and reduce.

Also, be very careful with currency. Don't get bit by floating point rounding. How can I format numbers as money in JavaScript?

Community
  • 1
  • 1
Brad
  • 159,648
  • 54
  • 349
  • 530
  • 1
    Don't you want him to try? – Smit Feb 24 '17 at 18:52
  • 1
    @Smit Of course I do. What's there to try with the code in the question? This person has no idea what they're doing. A little help at the beginning can get them moving. You should also note that I included some links for further reading. – Brad Feb 24 '17 at 18:54
  • As @Brad mentions please be very careful with floating point rounding. You may wish to round the `person.amount` before running the calculation or at least rounding it at the end. – Nicholas Koskowski Feb 24 '17 at 18:54