Andrew
Andrew

Reputation: 19

Putting an array in a variable for a if statement

I am trying to put the contents of an array into a variable to then use it in a if statement. My goal is to find the max number of a array and display it with the corresponding "month". I was able to achieve the max number but what should i = maxMonthIndex to so the if statement will work correctly?

function displayHighestSalary() {

    console.log("Inside displayArrayContents function");

    var maxFinal;
    var max = monthlySales[0];
    var maxMonth;
    var maxMonthIndex;

    console.log("monthlySales.length = " + monthlySales.length);


    //Loop through array and build display string
    for (var i = 0; i < monthlySales.length; i++)  
    {
        if (monthlySales[i] > max) {
            max = monthlySales[i];
            maxMonthIndex = **??????**;
        }
    }


    console.log("max = " + max)

    if (maxMonthIndex = monthlySales[0]) {
        maxMonth = "january";
    }
    else if (maxMonthIndex = monthlySales[1]) {
        maxMonth = "February";
    }
    else if (maxMonthIndex = monthlySales[2]) {
        maxMonth = "March";
    }
    else if (maxMonthIndex = monthlySales[3]) {
        maxMonth = "April";
    }
    else if (maxMonthIndex = monthlySales[4]) {
        maxMonth = "May";
    }
    else if (maxMonthIndex = monthlySales[5]) {
        maxMonth = "June";
    }
    else if (maxMonthIndex = monthlySales[6]) {
        maxMonth = "July";
    }
    else if (maxMonthIndex = monthlySales[7]) {
        maxMonth = "August";
    }
    else if (maxMonthIndex = monthlySales[8]) {
        maxMonth = "September";
    }
    else if (maxMonthIndex = monthlySales[9]) {
        maxMonth = "October";
    }
    else if (maxMonthIndex = monthlySales[10]) {
        maxMonth = "November";
    }
    else if (maxMonthIndex = monthlySales[11]) {
        maxMonth = "December";
    }

    maxFinal = addCommas(max);

    //Display results
    document.getElementById('divDisplayHighest').innerHTML = "$" + maxFinal + " " +     maxMonth;
}

Upvotes: 0

Views: 305

Answers (1)

lbstr
lbstr

Reputation: 2832

Set maxMonthIndex to i:

//Loop through array and build display string
for (var i = 0; i < monthlySales.length; i++)  
{
    if (monthlySales[i] > max) {
        max = monthlySales[i];
        maxMonthIndex = i;
    }
}

Change assignment statements to === and compare to the index (number) not the max value:

if (maxMonthIndex === 0) {
    maxMonth = "january";
}
// and so on

Even better:

// add this up top with all of your other variable declarations
var monthLabels = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];

//then...
//Loop through array and build display string
for (var i = 0; i < monthlySales.length; i++)  
{
    if (monthlySales[i] > max) {
        max = monthlySales[i];
        maxMonthIndex = i;
    }
}
// now that you know the maxMonthIndex, get the label
maxMonth = monthLabels[maxMonthIndex]; 

Upvotes: 2

Related Questions