Displaying desired range of a parallel array

76 Views Asked by At

I'm relatively new to coding and need a little bit of help with a homework problem. I have gotten pretty far in the problem, but I am struggling to display only the desired range in my output. My code runs well, but it just isn't giving me the output I need. I'll put my code down below under the question. Thank You!

Input a list of employee names and salaries stored in parallel arrays. The salaries should be floating-point numbers in increments of 100. For example, a salary of $36,000 should be input as 36.0 and a salary of $85,900 should be input as 85.9. Find the mean (average) salary and display the names and salaries of employees who earn within a range of $5,000 from the mean. In other words, if the mean salary is $45,000, all employees who earn between $40,000 and $50,000 should be displayed. NOTE: The values used in the scenario are for demonstration purposes; the values used in your program should be based on the floating-point datatype and increments of 100.

// Declare and initialize variables
var BR = "<br>"
var Name = []
var Sal = []
var Index = 0
var Sum = 0
var AverageSal = 0
var SP = "&nbsp; &nbsp; &nbsp; &nbsp; "

// Priming read
Name[Index] = prompt("Enter an employee name, or * to quit: ")

// Loop for more input until user enters * for employee name
while (Name[Index] != "*") {
    Sal[Index] = parseFloat(prompt("Enter the salary for that employee in increments of 100: "))
    Index++
    Name[Index] = prompt("Enter another employee name, or * to quit: ")
}

// Find the average Sal
for(Index = 0; Index < Sal.length; Index++) {
    Sum += Sal[Index];
}
AverageSal = Sum / Sal.length;

document.write(AverageSal + BR)

// Display desired output   
RangeMin = AverageSal -= 5
RangeMax = AverageSal += 5
Index = 0
while (Name[Index] != "*") {
    if (Sal[Index] >= RangeMin || Sal[Index] <= RangeMax) {
    document.write("Employee: " + Name[Index] + SP + "Sal: " + Sal[Index] + BR)
    }
    Index++
}
document.write(RangeMin + SP + RangeMax)
0

There are 0 best solutions below