`ForEach` only returns undefined and `map` and `reduce` do not work on this problem. How to make code work?! | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

`ForEach` only returns undefined and `map` and `reduce` do not work on this problem. How to make code work?!

My code needs to increase the elements in the salary by percent, calculate the accumulated salary in the funtion and return the total accumulated percents using the forEach Method in node js. https://code.sololearn.com/cp0F1jRsDnzc/?ref=app https://code.sololearn.com/cp0F1jRsDnzc/?ref=app

3rd Feb 2021, 9:00 AM
Emmanuel Riano
Emmanuel Riano - avatar
13 Answers
0
forEach should be used like a for loop to iterate over a target array... this is why there's no return value (undefined). from inside if the callback function, you could access outside variables (if they are in same scope than where the callback is created) as well as arguments (value, index, array)... so, one (pseudo) code solution could be: var r = 0; salaries.forEach((v,i,a) => { v *= percent/100; a[i] += v, r += v; }); console.log('accumulated percents: '+r); console.log('salaries: ['+salaries+']');
3rd Feb 2021, 5:07 PM
visph
visph - avatar
0
It still doesnt work. All it says is v is undefined
3rd Feb 2021, 5:49 PM
Emmanuel Riano
Emmanuel Riano - avatar
0
sory, forgotten parenthesis around arguments (v,i,a): corrected ;)
3rd Feb 2021, 5:55 PM
visph
visph - avatar
0
Excuse me if im blind but what parenthesis were forgotton
3rd Feb 2021, 6:11 PM
Emmanuel Riano
Emmanuel Riano - avatar
0
I initially wrote: forEach(v,i,a => { Now that's corrected to: forEach((v,i,a) => {
3rd Feb 2021, 6:13 PM
visph
visph - avatar
0
And when i run it it shows the accumulation for the first element then the array then the second element then the second array and so on
3rd Feb 2021, 6:13 PM
Emmanuel Riano
Emmanuel Riano - avatar
0
I just need one value from the array but it gives me more
3rd Feb 2021, 6:15 PM
Emmanuel Riano
Emmanuel Riano - avatar
0
I initially think to only write pseudo code, but I finally write real code... however, you shouldn't expect to have only to copy a working solution, but use my example to wite your own solution (where I had already made most of the work)
3rd Feb 2021, 6:17 PM
visph
visph - avatar
0
When i console.log r it prints the values i need but need to reduce them into one number. Do you have any ideas on how i can do that
3rd Feb 2021, 6:20 PM
Emmanuel Riano
Emmanuel Riano - avatar
0
And it returns a fifth value thats undefined. Ima keep working on it but this problem is insane
3rd Feb 2021, 6:24 PM
Emmanuel Riano
Emmanuel Riano - avatar
0
Thanks for helping it not return undefined. Ill leep workin n hope to debug it
3rd Feb 2021, 6:28 PM
Emmanuel Riano
Emmanuel Riano - avatar
0
Hey ive still been working on the problem it returns the value i need but returns undefined on all my code at the end on the value. I think im supposed to do a callback for the code soIi only get the values but i dont know how to do that. heres my code function main() { var percent = parseInt(readLine(),10); console.log(salaryIncrease(percent)); } var salaries = [3000, 7000, 5000, 15000]; const salaryIncrease = percent => { //your code goes here var r = 0; salaries.forEach((v,i,a) => { v *= percent/100; a[i] += v, r += v; }); console.log(r); }
10th Feb 2021, 10:53 PM
Emmanuel Riano
Emmanuel Riano - avatar