0

sorry if this question feels redundant, but I havent been able to find the answer with this method. It doesnt get answered on the link provided. I have been trying to make a Javascript program that checks if the input from the user is a Week day or weekend. It has to be done with for or do while loops.

I want my program to check if the user input is in the array, if it is, the program should be able to tell if it`s a working day or weekend.

My problem: It always returns working day, even if I put Saturday or Sunday.

this is tagged as Javascript, but works for any language since it`s basic stuff.

Here`s my code so far

var input = prompt("Enter a day of the week");
var day = ["monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"];

for (var i = 0; i <= day.length; i++)
{
    if(input >= day[5]){
        console.log("It´s weekend!"); {break;}
    }else
    {
        console.log("It´s a working day");

    }
}
Soviut
  • 88,194
  • 49
  • 192
  • 260
Koni
  • 452
  • 1
  • 7
  • 19
  • Possible duplicate of [How to find the array index with a value?](https://stackoverflow.com/questions/7346827/how-to-find-the-array-index-with-a-value) – Madhawa Priyashantha Jun 05 '17 at 23:49
  • 1
    What's your input? Is it a number or a full text (eg. Monday) – Joseph D. Jun 05 '17 at 23:49
  • My input is always a day, like "saturday" "sunday" "monday", etc. it returns working day always, even with numbers. – Koni Jun 05 '17 at 23:52
  • `console.log('It´s '+(/(satur|sun)day/.test(prompt('Enter a day of the week'))?'weekend!':'a working day'))` – le_m Jun 06 '17 at 00:00

2 Answers2

1

I don't think you have to use a for loop. The following code should work:

var input = prompt("Enter a day of the week");
var workingday = ["monday", "tuesday", "Wensday", "thursday", "friday"];
var weekend = ["saturday", "sunday"]

if (workingday.indexOf(input) != -1) {
     console.log("It´s a working day!");
} else if (weekend.indexOf(input) != -1) {
     console.log("It´s weekend!");
} else {
     console.log("Invalid input!");
}

By the way, you misspelled "Wensday". To use for loop:

for (var i = 0; i < day.length; i++)  {
    if (day[i] == input)  {
        console.log((i >= 5) ? "It's weekend!" : "It's a working day");
    }
}
X. Liu
  • 1,070
  • 11
  • 30
1

Your input is a string and your days are an array so you can't do a greater-than comparison in your if statement.

Instead, you need to find at what index (number) the day the input matches. There are already an array method called indexOf() finding the index of an item in an array. You can then see if that day number is greater than or equal to the days of the weekend.

var dayNumber = days.indexOf(input);
if (dayNumber >= 5) {
  // it's the weekend
}

However, if you have to use a for loop, you can simulate what indexOf() does. It works by increasing an number dayNumber each loop and then using that number to index into the days array. You can then compare the value at that index with your input. If it matches, you know what day number it is and can see if it's greater than or equal to the days of the weekend.

for (var dayNumber = 0; dayNumber < days.length; dayNumber++) {
  // check if the input matches the current index of the array
  if (input == days[dayNumber]) {
    // if it does, check if the day number is on the weekend
    if (dayNumber >= 5) {
      // it's the weekend
    }
  }
}

This can be made more compact using an "and operator" && to combine the two nested if statements.

if (input == days[dayNumber] && dayNumber >= 5) {
  // it's the weekend
}
Soviut
  • 88,194
  • 49
  • 192
  • 260