0

I'm stuck on a part of my JavaScript lab where I need to:

  • Prompt for user input
  • Then use a switch statement to display alert based on what was entered
  • Then check that its text and not a number

My teacher said to put it in a loop and I've used all of them so far on this problem but I just made myself more confused.

Here is my code:

<script>
        //array to compare input to
        var days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];

        do
        {
            //prompt user for input
            var response = prompt("Choose a day of the week: ");

                //loop through all array elements
                for(i in days)
                {
                        //choices that I will use to compare input to
                        switch(response)
                            {
                                case days[0]:
                                case days[1]:
                                case days[2]:
                                case days[3]:
                                case days[4]:
                                case days[5]:
                                case days[6]:
                                    alert("You have chosen day: " + days[i]);
                                    break;
                                default:
                                    alert("That is not a choice!");
                            }
                    }
            }//condition while input doesn't equal exactly array element prompt user again
        while(response !=== days[i]);
    </script> 
0w1
  • 1
  • 1
    You also have an additional `=` character that should be removed when comparing the response to the days array. Should just be `response !== days[i]`. – Carl Edwards Apr 22 '18 at 15:32

4 Answers4

0
var days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];

    do
    {
        //prompt user for input
        var response = prompt("Choose a day of the week: ");

            //loop through all array elements

                    //choices that I will use to compare input to
                    switch(response)
                        {
                            case days[0]:
                            case days[1]:
                            case days[2]:
                            case days[3]:
                            case days[4]:
                            case days[5]:
                            case days[6]:
                                alert("You have chosen day: " + days[days.indexOf(response)]);
                                break;
                            default:
                                alert("That is not a choice!");
                        }

        }//condition while input doesn't equal exactly array element prompt user again
    while(response !== days[i]);

There are two corrections

  1. One is no need of for loop, use the

indexOf

method to get the index of the input day in days

  1. Second one is the wrong use of relational operator !=== should be !==.
learn2code
  • 153
  • 2
  • 8
  • Thanks I tried taken out the extra = and the prompt started showing I knew the problem was with the loops but I just didn't know what about it. That extra loop made my alert keep showing until I took it out. – 0w1 Apr 22 '18 at 15:48
0

You're better off removing the for-in loop. Right now you have 2 loops with do-while and only one is needed. A better way to compare the array to the response would be to use indexOf and break the loop when there's a match.

//array to compare input to
var days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];

do {
  //prompt user for input
  var response = prompt("Choose a day of the week: ");

  //loop through all array elements
  //choices that I will use to compare input to
  switch (response) {
    case days[0]:
    case days[1]:
    case days[2]:
    case days[3]:
    case days[4]:
    case days[5]:
    case days[6]:
      alert("You have chosen day: " + response);
      break;
    default:
      alert("That is not a choice!");
  }
} //condition while input doesn't equal exactly array element prompt user again
while (days.indexOf(response) === -1);

This is a matter of taste but you could also clean this up by removing the switch statement and run this in a while loop:

var days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];

while(true) {
  var response = prompt("Choose a day of the week: ");
  
  var found = days.find(function(day) {
    return response === day;
  });

  if (found) {
    alert("You have chosen day: " + response);
    
    break;
  }

  alert("That is not a choice!"); 
}
Carl Edwards
  • 13,826
  • 11
  • 57
  • 119
  • So is there something wrong with !=== is wrong syntax or was I using it wrong also appreciate the help. – 0w1 Apr 22 '18 at 15:49
  • It's just incorrect syntax. You only use three equal signs when using strict equality. You use two when using strict non-equality. – Carl Edwards Apr 22 '18 at 15:55
0

Nice trick by @alejandro-martin ....

var days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
var daysToCompare = days.map(m => m.toLowerCase());
do {
    var response = (prompt("Choose a day of the week: ") || '').toLowerCase();
 switch(true) {
  case ((response = daysToCompare.indexOf(response)) >= 0):
   alert("You have chosen day: " + days[response]);
   response = true;
   break;
  default:
   alert("That is not a choice!");
   response = false;
 }
}
while(!response);
0
var daysList = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
isResponseValid = false;

do {
  var response = prompt('Choose a day of the week');
  if ( /^[0-9]*$/.test(response) ) {
    alert('You have key in a number, please key in a text');
  }
  else if( daysList.indexOf(response) >= 0){
    alert('You have chosen ' + response);
    isResponseValid = true;
  } else {
    alert('That is not a choice');
  }
} while (isResponseValid == false);

First line of if statement is to test for text. It doesn't make sense to test for number after you validate that the response is not part of your array.

The flow should be:

  1. Test for number, if it is number, alert the user that they have to key in text.

  2. If it is text, test to see if the text is inside the array.

  3. If test failed, alert users that input is not a choice.

Did not use a switch case here, because it doesn't seem to make too much sense to me. But above answers have provide examples to how you could use them in your code.

Added an additional var to test if response is valid, but I guess there is no added benefits from using response!==days[i]. :)

wing
  • 155
  • 6