gruffmeister
gruffmeister

Reputation: 29

Get number of days from month number

The biggest JS noob in UK trying to get the days in a month when you write 1-12 in a input.

Are there any awesome person who can tell me what I'm doing wrong?

JS :

window.onload = funkytown;

var mnd = [[0, "Jan"], [1, "Feb"], [2, "Mar"], [3, "Apr"], [4, "Mai"], [5, "Jun"], [6, "Jul"], [7, "Aug"], [8, "Sep"], [9, "Okt"], [10, "Nov"], [11, "Des"]];

var days = [[0, 31], [1, 28], [2, 31], [3, 30], [4, 31], [5, 30], [6, 31], [7, 31], [8, 30], [9, 31], [10, 30], [11, 31]];

function funkytown() {  
    document.getElementById("button").onclick = calender;
}
function calender() {
    var tall = parseint(document.getElementById)("box").value;
    if(tall >= 1 && tall <= 12) {
        var mndminus = tall - 1;

        for(i = 0; i < mnd.length; i++) {
            if(mnd[i][0] == mndminus) {
                document.getElementById("text").innerHTML = mnd[i][1] + " - " + days[i][1];
            }
        }
    }
}

HTML :

<input type="number" id="box" max="12" />
<input type="button" id="button" value="Trykk" />
<span id="text"></span>

Thanks!

Upvotes: 1

Views: 68

Answers (2)

Useless Code
Useless Code

Reputation: 12402

As dandavis's comment pointed out, February does not always have 28 days due to leap years. You would be better off creating a function to figure out how many days are in a month. Luckily, there is a relatively easy way to do this with JavaScript. If you set the date to 0 using either the Date constructor or Date.prototype.setDate, the resulting Date object will be set to the last day of the previous month, using this we can easily create a function to get the number of days in a month.

function getDaysInMonth(month) {
  var d = new Date();
  d.setMonth(month + 1);
  d.setDate(0);
  return d.getDate();
}

This function expects month to be zero-based like the Date constructor (meaning it expects month to be a number between 0 and 11 instead of 1 and 12).

  var days = getDaysInMonth(mndminus);

I would also recommend using a different data structure to look up the month name. Instead of looping through an array of two-element arrays, use an object as a dictionary.

var monthLookup = {
  0: "Jan",
  1: "Feb",
  2: "Mar",
  3: "Apr",
  4: "Mai",
  5: "Jun",
  6: "Jul",
  7: "Aug",
  8: "Sep",
  9: "Okt",
  10: "Nov",
  11: "Des"
};

Then looking up the month is as easy as monthLookup[mndminus].

With these changes your code would look something like this:

var getDaysInMonth = function (month) {
    var d = new Date();
    d.setMonth(month + 1);
    d.setDate(0);
    return d.getDate();
  },
  monthLookup = {
    0: "Jan",
    1: "Feb",
    2: "Mar",
    3: "Apr",
    4: "Mai",
    5: "Jun",
    6: "Jul",
    7: "Aug",
    8: "Sep",
    9: "Okt",
    10: "Nov",
    11: "Des"
  },
  calender = function () {
    var tall = parseInt(document.getElementById("box").value, 10),
      mndminus;

    if (tall >= 1 && tall <= 12) {
      mndminus = tall - 1;
      document.getElementById("text").innerHTML = monthLookup[mndminus] + " - " + getDaysInMonth(mndminus);
    }
  },
  funkytown = function () {
    document.getElementById("button").onclick = calender;
  };

window.onload = funkytown;

You'll notice that I used function declarations instead of function statements. This article does a good job of explaining what each of them are and why it's usually a good practice to use declarations instead of statements.

Something to lookout for, remember to always declare your variables! Your original code didn't declare i, which makes it an implicit global. Implicit globals can cause all sorts of weird bugs, especially if it is a common variable name like i that is likely to be used elsewhere in your code. Also, if you are using a loop to search an array, you can use the break statement to exit the loop early once you have found the item you are looking for. Without a break; statement your code tests all twelve items in the array no matter what month you are searching for, even if it is January and found on the first iteration.

I would also recommend learning to use JSHint or JSLint, they are great tools to help you improve your code. They are particularly good at spotting common errors like implicit globals, and will save you lots of time debugging by pointing those things out before they drive you mad.

Upvotes: 1

Zakaria Acharki
Zakaria Acharki

Reputation: 67505

You have just to replace this line :

var tall = parseint(document.getElementById)("box").value;

By :

var tall = parseInt(document.getElementById("box").value);

You have parseint that should be parseInt and ) after getElementById that should be in the end of line.

Hope this helps.


var mnd = [[0, "Jan"], [1, "Feb"], [2, "Mar"], [3, "Apr"], [4, "Mai"], [5, "Jun"], [6, "Jul"], [7, "Aug"], [8, "Sep"], [9, "Okt"], [10, "Nov"], [11, "Des"]];

var days = [[0, 31], [1, 28], [2, 31], [3, 30], [4, 31], [5, 30], [6, 31], [7, 31], [8, 30], [9, 31], [10, 30], [11, 31]];

function funkytown() {  
    document.getElementById("button").onclick = calender;
}
function calender() {
    var tall = parseInt(document.getElementById("box").value);
    if(tall >= 1 && tall <= 12) {
        var mndminus = tall - 1;

        for(i = 0; i < mnd.length; i++) {
            if(mnd[i][0] == mndminus) {
                document.getElementById("text").innerHTML = mnd[i][1] + " - " + days[i][1];
            }
        }
    }
}

funkytown();
<input type="number" id="box" max="12" />
<input type="button" id="button" value="Trykk" />
<span id="text"></span>

Upvotes: 1

Related Questions