+ 1

How to get .ForEach() to return first element of an array

I am trying to complete the Salary increase challenge for Javascript using .forEach() method. I am struggling to output the correct element for each of the tests. The expected input and answers are: 5, 1500 10,3000 50, 15000 My Code so far is passing the 3rd test only: function main() { var percent = parseInt(readLine(),10); console.log(salaryIncrease(percent)); } var salaries = [3000, 7000, 5000, 15000]; const salaryIncrease = percent => { //your code goes here salaries.forEach(v => { var increase = v*(percent/10); percent = v; }) return percent } I am unsure how to get it to return the right result for each different percent that is inputted into the function. I know the formula is right, as when I console.log(increase) it gives me the following answers: 1500, 3500, 2500, 7500. 3000, 7000, 5000, 15000 15000, 35000, 25000, 75000 How can I assign percent to be the first element of increase?

23rd Jan 2021, 12:53 AM
Zach Best
Zach Best - avatar
10 odpowiedzi
+ 3
So basically you need to calculate total increment of all the given salaries, from input percentage value. You should create an increase global variable, initialize it to zero before enter forEach loop iteration. var increase = 0; In the forEach loop, you only need to caculate each salary increment and add to increase valuable. salaryIncrement = v*(percent/100); increase += salaryIncrement; which is equivalent to increase = increase + salaryIncrement; After forEach loop, then return the increase variable to function. return increase; Here the code you can study: https://code.sololearn.com/WMeYSm1V9FGr/?ref=app
23rd Jan 2021, 11:16 AM
Calviղ
Calviղ - avatar
+ 6
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(salary => sum += salary * percent / 100); return sum; }
31st Mar 2021, 10:44 PM
Grethel Padilla
Grethel Padilla - avatar
23rd Jan 2021, 1:47 AM
Calviղ
Calviղ - avatar
+ 1
omg thank you. This was driving me insane. that makes perfect sense now that i need to declare increase outside the for loop first. i think i will remember this lesson better now as it took so long. sorry for the original bad explanations
23rd Jan 2021, 11:31 AM
Zach Best
Zach Best - avatar
0
That doesn't seem to work. It gives a console.log list of 10, 3300, 7700, 5500, 16500. I am trying to get the forEach loop return the first number of each of the following: 1500, 3500, 2500, 7500. 3000, 7000, 5000, 15000 15000, 35000, 25000, 75000 the value needs to be returned in the variable percent. The closest I can get is that it console logs the whole list of modified values and the original percentage that is inputted 1500 3500 2500 7500 5 // original percentage When I try to assign percent to be the variable increase it gives me a huge number. The 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 salaries.forEach(v => { var increase = v*(percent/10); percent = increase; } ) return percent } How can I fix this?
23rd Jan 2021, 10:04 AM
Zach Best
Zach Best - avatar
0
Zach Best I don't really get what you mean. Could you post your original challenge question?
23rd Jan 2021, 10:44 AM
Calviղ
Calviղ - avatar
0
Hi Sorry, I am failing to be clear. This is the information given by the original challenge: Functions in ECMAScript 6 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. Sample Input 10 Sample Output 3000 Use the forEach() function to operate with each element of the array. This is the starter code that I am given: function main() { var percent = parseInt(readLine(),10); console.log(salaryIncrease(percent)); } var salaries = [3000, 7000, 5000, 15000]; const salaryIncrease = percent => { //your code goes here } This is the code I have tried: function main() { var percent = parseInt(readLine(),10); console.log(salaryIncrease(percent)); } var salaries = [3000, 7000, 5000, 15000]; const salaryIncrease = percent => { //your code goes here salaries.forEach(v => { var increase = v*(percent/10); } ) return percent } In short I am struggling to get it to post the first item of the array salaries plus the increase by using a forEach loop. If I try and declare percent to equal increase outside the loop it says increase is undefined. if I do it inside the loop it creates a huge number. Though if I console.log(increase) in the loop it will give me a list of answers like: 1500, 3500, 2500, 7500. This first number of the list is the correct value. I don't know how to get it to just return this number outside the forEach loop and assign it to the variable percentage.
23rd Jan 2021, 11:00 AM
Zach Best
Zach Best - avatar
0
function main() { var percent = parseInt(readLine(),10); console.log(salaryIncrease(percent)); } var salaries = [3000, 7000, 5000, 15000]; const salaryIncrease = percent => { //your code goes here let increasetotal=0; salaries.forEach(salary=>{ increasetotal+=salary*percent/100; }); return increasetotal; }
8th Aug 2021, 4:21 AM
Daryl Wells
0
Here is my own answer: (it would help) function main() { var percent = parseInt(readLine(),10); console.log(salaryIncrease(percent)); } var total=0 var salaries = [3000, 7000, 5000, 15000]; const salaryIncrease = percent => { salaries.forEach(item=>{ total+=item*(percent/100); })
16th Sep 2021, 6:21 PM
Hasnae BOUHMADY
Hasnae BOUHMADY - avatar
0
the answer: var sum = 0; salaries.forEach(salary => sum += salary * percent / 100); return sum; }
19th Jan 2022, 5:40 PM
Anas AL Mallawi
Anas AL Mallawi - avatar