May someone help me figure this problem out. Ive studied forEach and arrow loops forever and still cant make my code work. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

May someone help me figure this problem out. Ive studied forEach and arrow loops forever and still cant make my code work.

function main() { var percent = parseInt(readLine(),10); console.log(salaryIncrease(percent)); } var salaries = [3000, 7000, 5000, 15000]; const salaryIncrease = percent => { //your code goes here if (salaryIncrease != undefined) salary = salaries.forEach(x => salaries * percent ) }

2nd Feb 2021, 12:35 AM
Emmanuel Riano
Emmanuel Riano - avatar
25 Answers
+ 2
That is my sloution !! 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 total = 0; salaries.forEach(i => { return total += i / 100 * percent } ); return total; }
17th May 2021, 9:16 PM
Capivo Group
Capivo Group - avatar
+ 2
//Please Subscribe to my youtube channel //channel name: Fazal Tuts4U function main() { var percent = parseInt(readLine(),10); console.log(salaryIncrease(percent)); } var salaries = [3000, 7000, 5000, 15000]; const salaryIncrease = percent => { let total = 0; salaries.forEach(i => { return total += i / 100 * percent } ); return total; }
5th Sep 2021, 4:47 PM
Fazal Haroon
Fazal Haroon - avatar
0
What output you got using forEach? Have you tried using `map` method instead? function main() { var percent = parseInt( readLine(), 10 ); percent =1.0 + ( percent / 100 ); var salaries = [ 3000, 7000, 5000, 15000 ]; const salaryIncrease = percent => salaries.map( s => s * percent ); console.log( salaryIncrease( percent ) ); }
2nd Feb 2021, 12:57 AM
Ipang
0
Lol no it said to iterate using the forEach method but kept returning undefined. Appreciate it foreal
2nd Feb 2021, 5:28 AM
Emmanuel Riano
Emmanuel Riano - avatar
0
This gives me a new problem on reducing them all together and only giving me the percentage values added together though.
2nd Feb 2021, 5:33 AM
Emmanuel Riano
Emmanuel Riano - avatar
0
I can't see PRO challenges, so I don't know the task. I thought it was supposed to just increase the elements of array <salaries> by <percent>. If you must use `forEach`, then you can use the optional argument (index) in the callback function you pass to `forEach` method as you invoke it. Use the index to refer an element to modify directly from <salaries>, modify it, then return modified <salaries> from the `salaryIncrease` function. I guess you can do the percentage accumulation also in the `salaryIncrease` function. I haven't tried, but I think it's doable. Here's info about `forEach`. If you see the return value section, you'll see that it returns `undefined` - the reason why you got `undefined` in console. https://www.w3schools.com/jsref/jsref_foreach.asp
2nd Feb 2021, 6:52 AM
Ipang
0
Any idea on how to reduce this function function main() { var percent = parseInt(readLine(),10); percent =1.0 + (percent / 100); var salaries = [3000, 7000, 5000, 15000]; const salaryIncrease = percent => //your code goes here salaries.map( s => (s * percent)- s ); console.log(salaryIncrease(percent)); }
3rd Feb 2021, 6:07 AM
Emmanuel Riano
Emmanuel Riano - avatar
0
Basically your code but i got the array to return the values i want but still need the sum
3rd Feb 2021, 6:08 AM
Emmanuel Riano
Emmanuel Riano - avatar
0
I recall you said challenge required you to use `forEach`? What sum again? I think you need to copy/paste the task Description.
3rd Feb 2021, 6:31 AM
Ipang
0
Ok it says for me to use forEach but it doesnt limit me from using other functions. so basically i need to get all elements in an array, multiply it by the percent, and add the percents together as one value. So its like i need map to get the array of percent amounts and to get the sum of them all together. Ive even reduced it instead of using map but it only gets the first element and .length only returns how many elements there is
3rd Feb 2021, 6:36 AM
Emmanuel Riano
Emmanuel Riano - avatar
0
Lets say for this problem im not using forEach because its stupid and returns undefined and cant figure out how to get it to program instructions.
3rd Feb 2021, 6:38 AM
Emmanuel Riano
Emmanuel Riano - avatar
0
Thisis the problem 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.
3rd Feb 2021, 6:39 AM
Emmanuel Riano
Emmanuel Riano - avatar
0
This is the original problem function main() { var percent = parseInt(readLine(),10); console.log(salaryIncrease(percent)); } var salaries = [3000, 7000, 5000, 15000]; const salaryIncrease = percent => { //your code goes here }
3rd Feb 2021, 6:42 AM
Emmanuel Riano
Emmanuel Riano - avatar
0
this returns the percents array. i think map only returns a new array and you cant reduce it though function main() { var percent = parseInt(readLine(),10); percent =1.0 + (percent / 100); var salaries = [3000, 7000, 5000, 15000]; const salaryIncrease = percent => //your code goes here salaries.map( s => (s * percent)- s ); console.log(salaryIncrease(percent)); }
3rd Feb 2021, 7:01 AM
Emmanuel Riano
Emmanuel Riano - avatar
0
Ok, let me sum it up, correct me if I'm wrong; 1. Increase all the elements in <salaries> array by <percent> 2. Inside `salaryIncrease` function, calculate the salary increment of each element, and return the accumulated salary increment. 3. Use `forEach` method. 4. Code in Node.js (or Javascript - tagged). Is that it?
3rd Feb 2021, 7:04 AM
Ipang
0
Yes!
3rd Feb 2021, 7:05 AM
Emmanuel Riano
Emmanuel Riano - avatar
0
You broke it down great btw
3rd Feb 2021, 7:05 AM
Emmanuel Riano
Emmanuel Riano - avatar
0
Ok this is how I'd do it by using `forEach` method. function main() { var percent = parseInt( readLine(), 10 ); const salaries = [ 3000, 7000, 5000, 15000 ]; const salaryIncrease = percent => { var bonusAccumulation = 0; percent /= 100; salaries.forEach( (element, index, array ) => { element *= percent; bonusAccumulation += element; array[ index ] += element; }); return bonusAccumulation; } console.log( salaries ); // original console.log( salaryIncrease( percent ) ); // total salary increment console.log( salaries ); // added percent } main();
3rd Feb 2021, 7:18 AM
Ipang
0
That returns the first array of salaries and a second array of salaries plus percent
3rd Feb 2021, 8:16 AM
Emmanuel Riano
Emmanuel Riano - avatar
0
Save and share your code link here. I can't imagine what you said. https://www.sololearn.com/post/75089/?ref=app
3rd Feb 2021, 8:36 AM
Ipang