Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8261509
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T03:26:00+00:00 2026-06-08T03:26:00+00:00

I am trying to put the contents of an array into a variable to

  • 0

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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T03:26:02+00:00Added an answer on June 8, 2026 at 3:26 am

    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]; 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to use .push to put items into a javascript array. I
I'm trying put an if statement directly into a select field in rails, with
I'm trying to put some custom list adapters into their own classes to make
I'm grabbing various jquery objects to put into an array and later spit out
Ok I am trying to take a string and put it into the intent
I'm trying to put a TextView into my TableView so that it doesn't push
I am trying to print an array into a file staticSurveyDataFileTest.php . I am
I am trying to insert an array into a php file . However I
im trying to insert an array values into the file using foreach.. and if
I am trying to decode some JSON into a php array. Here's the code

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.