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);
6 Answers 6
You don't need to use for loop, just return the value
return months[i - 1];
Comments
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
Comments
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];
}
Comments
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 ];
}
Comments
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);
Comments
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>
for loopindexas an argument into the function and returnmonth[index]getMonthNamefunction. Also the months array should not contain the wordMonth"Zerouary"if it really bugs you. In most programming languages arrays are zero indexed, so it's something you should get used to.