Confused on this ES6 Function Practice Problem | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Confused on this ES6 Function Practice Problem

Here is the problem and the code I have written so far: You have created an array containing the salaries of your factory workers. The manager at the factory has decided to give salary raises to his best workers and needs to the see the impact of these increases on the budget. The program you are given takes the salary percent increase as input. Complete the given function to use the percent as a parameter, then calculate and return the total salary increase for all of the workers in the array. function main() { var percent = parseInt(readLine(),10); console.log(salaryIncrease(percent)); } var salaries = [3000, 7000, 5000, 15000]; let count = 0; const salaryIncrease = percent => { //your code goes here salaries.forEach(v => { console.log(v*(percent/100)); }); }; When I use "console.log(v*(percent/100))" it outputs the percentage increase for each salary (150, 350, 250, 750), but I'm not quite sure how to get the sum of those increases to get to the answer. Would love if someone could explain how to get the sum in this scenario. Also, the problem specifically asks for me to use the forEach() function.

31st Mar 2021, 7:17 AM
Arthur Laffer
3 Answers
+ 2
This is how you can do it. var salaries=[1000,2000,3000] var sum=0; salaries.forEach(v=>{ sum+=(v*(100+percent ))/100-v; }); console.log(sum)
31st Mar 2021, 9:06 AM
Hima
Hima - avatar
+ 4
In salaryIncrease(), 1. Declare a variable called sum and initialize it as 0. 2. In forEach callback, instead of printing the individual increase, add it to the sum. 3. After the forEach, return the sum.
31st Mar 2021, 8:53 AM
Gordon
Gordon - avatar
+ 1
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 sum=0; salaries.forEach(v=>{ sum+=(v*(100+percent ))/100-v; }); return sum; } Good Luck
25th Jan 2022, 8:50 AM
Muhammad Alif Deva Rizqon
Muhammad Alif Deva Rizqon - avatar