1

I am trying to get the actual month from a loop. When I step through it in developer tools and month[i] == 4 it doesn't assign actualMonth to checkMonth

Do I have to assign getMonth to month[] and then try and query the value?

var showCurrentMonth = function() {
    var getMonth = new Date().getMonth();

    var month = ["january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december"];

    var actualMonth = "";
    for (var i = 0; i < month.length; i++) {
        var checkMonth = month[i];
        console.log(month[i]);
        if (getMonth == month[i]) {
            actualMonth = checkMonth;
        }
    }
    console.log(actualMonth);
}
window.addEventListener('DOMContentLoaded', showCurrentMonth, false);  
1

4 Answers 4

2

Too simple?

var showCurrentMonth = function() {
    var getMonth = new Date().getMonth();

    var month = ["january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december"];

    var actualMonth = month[getMonth];
    
    console.log(actualMonth);
}
window.addEventListener('DOMContentLoaded', showCurrentMonth, false);  

Sign up to request clarification or add additional context in comments.

1 Comment

just what I wanted :)
2

Change your if to if (month[getMonth] == month[i]) {

Do it like this :

 var showCurrentMonth = function() {
    var getMonth = new Date().getMonth();

    var month = ["january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december"];

    var actualMonth = "";
    for (var i = 0; i < month.length; i++) {
        var checkMonth = month[i];
        console.log(month[i]);
        if (month[getMonth] == month[i]) {  //Compare get month like this
            actualMonth = checkMonth;
        }
    }
    console.log(actualMonth);
}
window.addEventListener('DOMContentLoaded', showCurrentMonth, false);  

Comments

2

Your problem is that you are comparing an integer and string:

var getMonth = new Date().getMonth(); // This return number from 0 to 11

The code Date().getMonth() returns an integer, and your month list has strings on it

Your code should be:

var showCurrentMonth = function() {
    var getMonth = new Date().getMonth();

    var month = ["january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december"];

    var actualMonth = month[getMonth];
    console.log(actualMonth);
}
window.addEventListener('DOMContentLoaded', showCurrentMonth, false); 

To get the actual month you only need to access the month list with getMonth as index

2 Comments

actually getMonth() returns value between 0 and 11: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
Yup, you're right I was changing the answer because the boucle is not needed there, he only needs to access the month list with the getMonth as index
1

slightly Modification required

var showCurrentMonth = function() {
        var getMonth = new Date().getMonth();
        console.log(getMonth);
        var month = ["january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december"];

        var actualMonth = "";
        for (var i = 0; i < month.length; i++) {
            var checkMonth = month[i];

            if (getMonth == i) {
                actualMonth = checkMonth;
            }
        }
        console.log(actualMonth);
    };
    window.addEventListener('DOMContentLoaded', showCurrentMonth, false);

1 Comment

The boucle is not needed, the only need there is to access the month list with the getMonth value as index, no need to enter in a boucle of month length

Your Answer

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.