1

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);
asked Feb 23, 2016 at 19:02
5
  • 1
    You don't need to use for loop Commented Feb 23, 2016 at 19:05
  • 1
    Why are you using a for loop? All you have to do is pass an index as an argument into the function and return month[index] Commented Feb 23, 2016 at 19:05
  • You shouldn't need to loop through anything inside of the getMonthName function. Also the months array should not contain the word Month Commented Feb 23, 2016 at 19:06
  • Thank you but I don't know how to account for indexing starting at 0 without adding an extra word. Commented Feb 23, 2016 at 19:09
  • You can add "Zerouary" if it really bugs you. In most programming languages arrays are zero indexed, so it's something you should get used to. Commented Feb 23, 2016 at 19:41

6 Answers 6

1

You don't need to use for loop, just return the value

return months[i - 1];
answered Feb 23, 2016 at 19:06
Sign up to request clarification or add additional context in comments.

Comments

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

answered Feb 23, 2016 at 19:08

Comments

1

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];
}
answered Feb 23, 2016 at 19:05

Comments

0

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 ];
}
answered Feb 23, 2016 at 19:08

Comments

0

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);
answered Feb 23, 2016 at 19:10

Comments

0

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/

answered Feb 23, 2016 at 19:48

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.