我试图将数组的内容放入一个变量中,然后在 if 语句中使用它。我的目标是找到一个数组的最大数量并用相应的“月份”显示它。我能够达到最大数量,但是 i = maxMonthIndex 应该怎样才能使 if 语句正常工作?
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;
}