0

I just learned about JavaScript and here is my script:

var now = new Date();
var date = now.getDate();
var month = now.getMonth();
var Holidays = [
    [8, 3],
    [9, 8],
    [10, 16],
    [11, 7],
    [11, 24],
    [11, 25],
    [11, 26],
    [11, 27],
    [11, 28],
    [11, 29],
    [11, 30],
    [11, 31],
    [0, 1],
    [0, 2],
    [0, 3],
    [0, 4],
    [0, 31],
    [1, 15],
    [1, 18],
    [2, 11],
    [2, 12],
    [2, 13],
    [2, 14],
    [2, 15],
    [2, 29],
    [3, 1],
    [4, 20],
    [5, 26],
    [5, 27],
    [5, 28]
];
var i = 0;
while (i <= Holidays.length) {
    if (check() === true) {
        console.log("No school today.");
        i = 32;
    } else if (check() === false) {
        if (i < Holidays.length) {
            i++;
        } else {
            console.log("we work today.");
            i++;
        }
    }
}

function check() {
    if (month == Holidays[i][0] && date == Holidays[i][1]) {
        return true;
    } else {
        return false;
    }
}

The purpose is to make it print out "No school today" for the days in the holiday array, otherwise, it'd print out "we work today". Whenever I run the script it always says

type error Holidays[i] undefined

Can someone help me with this?

2 Answers 2

1

At least one problem in your code

while (i <= Holidays.length) {

the last i should be Holidays.length-1, so use:

while (i < Holidays.length) {
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks but i only want it to print out "we work today" only once after everything has been checked. Can you help me?
1

The last time the code does an i++, i will end up with a value that is the last index of Holidays + 1.

So in the line that throws an error, you're trying to retrieve an item that's not in the array. You're not trying to get a specific element of the array and the offending code is not in a loop, so you might want to check that.

Comments

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.