+ 2

JAVASCRIPT: 57.3 Practice - Salary Rise

In JavaScript Course, under ECMAScript 6 > 57.3 Practice. You need to write a program (using ES6 function/syntax) to calculate the total increase for each employee's salary in an array. using the percent as an input. Here's the given array: var salaries = [3000, 7000, 5000, 15000]; Let's say the input is 10 (percent), the 10 percent of each array is as follows: 300, 700, 500, 1500 Which in total, 3000. The program needs to output the TOTAL ONLY, I'm having a hard time to figure out how I can display the total only. I was able to display the percent increase for each employee. Please see my code below: 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 => { v = (v * percent) / 100; console.log(v); }); } Sample Input: 5 My Output: 150 350 250 750 undefined Expected Output: 1500 I know I'm not supposed to print each values, but I'm having a hard time on how I can display the sum/total of each value that I printed.

9th Oct 2021, 8:48 PM
Rolly U. Valdemoro Jr.
Rolly U. Valdemoro Jr. - avatar
3 Antworten
+ 5
Addition Assignment Operator in Java Script is the answer you are looking for. This is simply a += operation on v. You will want to change v = (v * percent) / 100; to v += (v * percent) / 100;
9th Oct 2021, 8:53 PM
Jibraeel Abdelwahhab
Jibraeel Abdelwahhab - avatar
+ 2
Hi Jibraeel, Followed your advice but created a variable first for the total and it works!! Thank you! Here's the final 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 let total = 0; salaries.forEach(v => { total += (v * percent) / 100; }); return total; }
9th Oct 2021, 9:00 PM
Rolly U. Valdemoro Jr.
Rolly U. Valdemoro Jr. - avatar
+ 1
Fixed it :D
9th Oct 2021, 9:22 PM
Rolly U. Valdemoro Jr.
Rolly U. Valdemoro Jr. - avatar