Here's a beginner JS question. I'm making a simple app that takes the days I work, the day of the week and then alert's me if I work today.
//
var daysIWork = [
'Wednesday',
'Friday',
'Saturday',
'Sunday',
];
var dayToday = 'Saturday'; // I'll be changing this to a more advanced method, getDay? When I've figured out how to post this correctly.
for (var i = 0; i < daysIWork.length; i++) {
console.log(daysIWork[i])
}; // This posts the array correctly, yay.
This is where I'm stuck. If I input a number that matches an array number ie [1] instead of [i], the statement will pop up true. However with the code below, it always turns out false.
if (dayToday === daysIWork[i]) {
alert ('Noooo, you work today!');
} else {
alert ("Yes, you don't work today!");
}
I know how to get it working using the array call [1] [2] etc but I can't get it to read a whole array and come back as true.
Thanks.
if? It works fine when placing it in the loop: jsfiddledayTodayis withindaysIWorkor not. So you actually would usedaysIWork.indexOf(dayToday) !== -1to check if it is in the array. For IE 8 and before your would need to use a polyfill MSN: Array.prototype.indexOf().daysIWork.indexOf(dayToday) !== -1?