4

I am trying to add multiple arrays in javascript. Here are my arrays I have made, and are working.

function getAmountSpent(){
 var amountSpent = ((Math.random() * 500) + 1);
 return amountSpent.toFixed(2)
}
function getGift(){
 var gift = ((Math.random()* 50) + 1);
 return gift.toFixed(2)
}
var names = ["Jeremy","Arun","Alisa","Rohan","Dana"];
var spent = [];
for (let i = 0; i < 5; i++) {
spent.push(getAmountSpent());
}
var gifts = [];
for (let i = 0; i<5; i++) {
gifts.push(getGift());
}

What I need help with is adding these arrays in a new function. I have began writing the code, and I am not sure what is wrong.

var totals =[];
for (let i=0; i<5; i++) {
totals.push(getSumTotals())
}
function getSumTotals(a){
 totals= spent+(spent * gifts);
 return totals.toFixed(2)
}

From what you can see, I am trying to add up the totals much like this:

totals[0] = spent[0] + (spent[0] * gifts[0]);
totals[1] = spent[1] + (spent[1] * gifts[1]);
totals[2] = spent[2] + (spent[2] * gifts[2]);
totals[3] = spent[3] + (spent[3] * gifts[3]);
totals[4] = spent[4] + (spent[4] * gifts[4]);

if it helps, the professor added guided instructions for function getSumTotals(a) stating:

 This function will return the sum of the elements in array a.
 You will be passing the array that holds your totals to
 the parameter a. Be sure to treat the values in a as numbers.

the table

I am not sure if this helps but here is the output to my document. Current Total should equal (spent) + (spent * gifts). For instance, for Jeremy in this example, current total should equal: 36ドル.55 + (36ドル.55*0.0626) = 38ドル.83. Since there are many variables involved, I am not 100% sure what I should write for function getSumTotals(a)

The parameter "a" is a placeholder because I am not sure how many parameter values I need, and the proper format I need to use.

asked Oct 30, 2019 at 4:08
3
  • You haven't passed anything into the getSumTotals function, but it's expecting a parameter. Is that your issue? Commented Oct 30, 2019 at 4:14
  • i think this is the issue. Commented Oct 30, 2019 at 4:23
  • Actually I believe the issue is that your parameter is completely unused. Your "getSumTotals" function does not use "a" at all. Commented Oct 30, 2019 at 4:24

5 Answers 5

1

As for the code...

You're both

  1. not passing an index to getSumTotals
  2. not using this parameter within getSumTotals to access your spent and gifts arrays
var totals =[];
for (let i=0; i<5; i++) {
 totals.push(getSumTotals(i)) // you were missing i
}
function getSumTotals(idx) { // I took liberties to rename this
 totals = spent[idx] + (spent[idx] * gifts[idx]);
 return totals.toFixed(2);
}

Now for the Math...

All that said, this math of spent[i] + spent[i] * gifts[i] doesn't make much sense either. Was this specified in the problem?

answered Oct 30, 2019 at 4:29
Sign up to request clarification or add additional context in comments.

Comments

0

you may use like this

defined gifts

gifts=[45,43,32];

defined spends

spends=[43,32,21];

this is the getSumTotal funtion

getSumTotal=(x)=>(x.a+x.b)

this is where added

totals=gifts.map((d1,i)=>{
 return fu({a:gifts[i],b:spends[i]})
})
answered Oct 30, 2019 at 4:29

Comments

0

I understand this is your assignment, however - if the idea is to both generate arrays, and then add them together, it is a redundant step. Just use the name array to iterate once and do all your calculations within that single loop.

Here, I had some fun and took some liberties, but hopefully you see why multiple arrays are redundant.

function getSumTotals() {
 const getAmountSpent = () => Math.random() * 500 + 1;
 const getGift = () => Math.random() * 50 + 1;
 const names = ["Jeremy", "Arun", "Alisa", "Rohan", "Dana"];
 
 let totals = []
 names.forEach((name, i) => {
 let spent = getAmountSpent()
 let gifts = getGift()
 let $$$ = (spent + spent * gifts).toFixed(2);
 totals[i] = $$$
 console.log(`${name} cost me $${$$$}${'!'.repeat(($$$/1000) | 1)}`)
 });
 return totals;
}
getSumTotals()

answered Oct 30, 2019 at 4:53

Comments

0

Note, that toString returns a type of "String", but not "Number".
When you try to sum a number with string, you get a concatenated string "1" + 2 = "12"
To turn a string into Number, you must use a Number("str") function, or just a bunary + before the string:

console.log( "1" + 2 );
console.log( Number("1") + 2 );
console.log( +"1" + 2 );

Also, you use the same loop 3 times, but can use just one loop instead, and call all functions inside the one loop. And use your array.length instead of fixed number 5:

let names = ["Jeremy", "Arun", "Alisa", "Rohan", "Dana"];
let spent = [];
let gifts = [];
let totals = [];
for (let i = 0; i < names.length; i++) {
 spent.push( getAmountSpent() ); 
 gifts.push( getGift() ); 
 totals.push( getSumTotals(i) );
}
console.log( totals );
function getAmountSpent() {
 return rand(1, 500, 2);
}
function getGift() {
 return rand(1, 50, 2);
}
function getSumTotals(i) {
 return +( spent[i] * ( 1 + gifts[i] ) ).toFixed(2);
}
function rand(from, to, fixed = 0){
 return +(Math.random()*( to - from ) + from).toFixed(fixed);
}

P.s. Math.random() returns a number between 0 (included) and 1 (not included). If you need a random number between (example) 20 and 100, Math.random()*(100-20) will give a number between 0 and 80. After adding +20 to the result, you get a number from 20 to 100. That's what does this formula Math.random()*( to - from ) + from

P.P.s. Another way, to get the same thing:

var names = ["Jeremy", "Arun", "Alisa", "Rohan", "Dana"].reduce( (prev, elem) => {
 let spent = rand(1, 500, 2);
 let gift = rand(1, 50, 2);
 prev[elem] = new UserData( spent, gift );
 return prev;
}, {});
console.log( "Jeremy spent: " + names.Jeremy.spent );
console.log( names );
function rand(from, to, fixed = 0){
 return +(Math.random()*( to - from ) + from).toFixed(fixed);
}
function UserData(spent, gift){
 this.spent = spent;
 this.gift = gift;
 this.total = +(spent * ( 1 + gift )).toFixed(2);
}
/* Google → Array reduce, Constructor functions */

answered Oct 30, 2019 at 4:52

2 Comments

yeah I realized that I used the same loop 3 times, I just am following the directions stated for the assignment. Im sure the professor would not mind this sort of method.
when I run this on my document, the totals are not displayed, however the customer name, amount spent, and gift cards work.
0

 function getAmountSpent(){
 let amountSpent = ((Math.random() * 500) + 1);
 return Number(amountSpent.toFixed(2)) 
 }
 function getGift(){
 let gift = ((Math.random()* 50) + 1);
 return Number(gift.toFixed(2))
 }
 let names = ["Jeremy","Arun","Alisa","Rohan","Dana"];
 let spent = [];
 let gifts = [];
 let totals =[];
 for (let i = 0; i < names.length; i++) {
 spent.push(getAmountSpent());
 gifts.push(getGift());
 totals[i] = (spent[i]+(spent[i] * gifts[i])).toFixed(2);
 totals[i] = parseFloat(totals[i])
 }

Hi there I don't think you need a function to add the totals. you just need to loop through and assign totals[i] to spent[i] + (spent[i] * gifts[i]). then you can use the parseFloat and toFixed function to change the string to a number. remember toFixed() function turns numbers to string. so you need to use the parseFloat to change it to number again as shown in the code above. or you can come up with an easy way of changing it to number. I hope this helps!

answered Oct 30, 2019 at 5:54

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.