-3

I use this with HTML and every time I write anything, even words that aren't in 'days', it outputs the same values from the first section: "12pm-1pm", "6pm" and "8am"

    var CurrentDay = window.prompt('what day is it?');

    var days = ['Monday', 'Teusday', 'Wednesday', 'Thursday', 'Friday', 'Sataurday', 'Sunday'];

    var lunchTime = 0;
    var EndTime = 0;
    var StartTime = 0;

    if (CurrentDay === days[1] || days[2] || days[3]) {
        lunchTime = "12pm-1pm";
        EndTime = "6pm";
        StartTime = "8am";}

    else if (CurrentDay === days[0]) {
        lunchTime = "12pm-1pm";
        EndTime = "6pm";
        StartTime = "9am";}

    else if (CurrentDay === days[4] || days[5]) {
        lunchTime = "12pm-1pm";
        EndTime = "6pm";
        StartTime = "10am";}

    else if (CurrentDay === days[6]) {
        lunchTime = "12:30pm-2pm";
        EndTime = "6pm";
        StartTime = "10am";}

    alert(lunchTime);
    alert(EndTime);
    alert(StartTime);
FaithlessS
  • 17
  • 5
  • Tuesday and Saturday are misspelled – depperm Apr 16 '21 at 12:17
  • 2
    `days[2]` (`'Wednesday'`) is truthy, so `|| days[2]` is always true. – deceze Apr 16 '21 at 12:17
  • You closed the question before I'd finished. "I've always found `if statements` very difficult to handle. So ignoring the typos that the others have pointed out, maybe I could interest you in a nice `switch` statement instead. It's slightly easier to understand. [Here's the code](https://jsfiddle.net/ykrcqfbL/)." – Andy Apr 16 '21 at 12:33

1 Answers1

3

You should add the condition like:

if (CurrentDay === days[1] || CurrentDay === days[2] || CurrentDay === days[3])

var CurrentDay = window.prompt('what day is it?');

    var days = ['Monday', 'Teusday', 'Wednesday', 'Thursday', 'Friday', 'Sataurday', 'Sunday'];

    var lunchTime = 0;
    var EndTime = 0;
    var StartTime = 0;

    if (CurrentDay === days[1] || CurrentDay === days[2] || CurrentDay === days[3]) {
        lunchTime = "12pm-1pm";
        EndTime = "6pm";
        StartTime = "8am";}

    else if (CurrentDay === days[0]) {
        lunchTime = "12pm-1pm";
        EndTime = "6pm";
        StartTime = "9am";}

    else if (CurrentDay === days[4] || CurrentDay === days[5]) {
        lunchTime = "12pm-1pm";
        EndTime = "6pm";
        StartTime = "10am";}

    else if (CurrentDay === days[6]) {
        lunchTime = "12:30pm-2pm";
        EndTime = "6pm";
        StartTime = "10am";}

    alert(lunchTime);
    alert(EndTime);
    alert(StartTime);
Mamun
  • 66,969
  • 9
  • 47
  • 59