简体   繁体   中英

Having trouble returning an element of an array

So I'm doing a free online javascript course. I'm on lesson 2. It focused on arrays, objects and events. I did well on the quiz but have no idea how to do the assigment. Here is the assignment.

Create an array containing "January" through "December" Create a function called GetMonthName that takes a single number as a parameter and returns the name of that month. For example:

getMonth(3); //will return Month

Remember that arrays are indexed starting with 0. but here, month 1 should be January. So you'll have to account for that somehow.

So here's my code. Am I on the right track at all? Any help would be appreciated.

//create array
var months = ["Month","January","February","March","April","May","June","July","August","September","October","November","December"];

//create function
function getMonthName(month) {
    for(i=0;i<=12;i++) {
        var getMonth=months[i];
        return getMonth;
    }
}

//call function
getMonthName(getMonth);

You don't want to be using a loop here, since you're only returning one month. You likely want something like:

function getMonthName(month) {
   var getMonth=months[month];
   return getMonth;
}

or simpler:

function getMonthName(month) {
   return months[month];
}

To deal with 0-indexing, you can also remove 'Month' from your array, and just modify your index:

function getMonthName(month) {
   return months[month-1];
}

您不需要使用for循环,只需返回值

return months[i - 1];

You don't need to have "Month" in the array. Try doing something like this

//create array
var months = ["January","February","March","April","May","June","July","August","September","October","November","December"];

//create function
function getMonthName(month) {
    return months[month-1]
}

This way you don't have to add anything to the array except month names

You're returning from the loop without any conditions, so you'll always return the first element in the array and stop the loop.

If it's some kind of exercise in using loops, then you should only return when a condition is met, like

function getMonthName(month) {
    for(i=0;i<=12;i++) {
        var getMonth=months[i];
        if( i == month ){
            return getMonth;
        }
    }
}

But if you really want to get a month from array by its index, and have it 1-based so getMonthName(1) == "January" then:

var months = ["January","February","March","April","May","June","July","August","September","October","November","December"];
function getMonthName(month) {
  return months[ month + 1 ];
}

lets see what you could have done:

//create array
var months = ["Month","January","February","March","April","May","June","July","August","September","October","November","December"];

Here on the function first i make sure my parameter is integer then i check if the number passed is between 1 and 12 and at last i return the chosen month based on the number passed less one.

//create function
function getMonthName(month) {
    month = (parseInt(month) || 0) -1;
    return month>0 && month<13 ? months[month] : null;
}

//call function
getMonthName(1);

 var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]; var getMonth = (function(index){ return this[index-1]; }).bind(months); var month = getMonth(1); document.getElementById('month').innerHTML = month; 
 <div id="month"></div> 

Example: https://jsfiddle.net/artamonovdev/s80rx6fg/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM