1

I have a switch statement. It almost works fine, however instead of just showing one case, it shows the selected case then the default. Here is my code:

var people = {
  names: ["Sam", "Tim", "Steve"],
  emails: ["sam@email.com", "timm@messages.org", "stevieG@youhavemail.com"],
  phonenums: [1111, 2222, 4545]
}

var search = prompt("Type in someone's name to find their phone number and email.");

switch (search) {
  case people.names[0]:
    alert(people.names[0] + "'s email: " + people.emails[0] + " phone number: " + people.phonenums[0]);
  case people.names[1]:
    alert(people.names[1] + "'s email: " + people.emails[1] + " phone number: " + people.phonenums[1]);
  case people.names[2]:
    alert(people.names[2] + "'s email: " + people.emails[2] + " phone number: " + people.phonenums[2]);
  default:
    alert("I don't know that person.");
}

Why does this happen?

theratkeeper
  • 67
  • 2
  • 10

3 Answers3

5

Because you don't have any break in your switch cases.

Check the documentation for the switch statement on MDN. It says the following about break (emphasis mine)

The optional break statement associated with each case label ensures that the program breaks out of switch once the matched statement is executed and continues execution at the statement following switch. If break is omitted, the program continues execution at the next statement in the switch statement.

So update your cases to look like

case people.names[0]:
  alert(people.names[0] + "'s email: " + people.emails[0] + " phone number: " + people.phonenums[0]);
  break;
Daniel
  • 10,641
  • 12
  • 47
  • 85
3

You need to put breaks in a switch statement if you don't want the default to run. Just do this:

var people = {
    names : ["Sam", "Tim", "Steve"],
    emails : ["sam@email.com", "timm@messages.org", "stevieG@youhavemail.com"],
    phonenums : [1111, 2222, 4545]
}

var search = prompt("Type in someone's name to find their phone number and email.");
switch(search) {
    case people.names[0]:
        alert(people.names[0] + "'s email: " + people.emails[0] + " phone number: " + people.phonenums[0]);
        break;

    case people.names[1]:
        alert(people.names[1] + "'s email: " + people.emails[1] + " phone number: " + people.phonenums[1]);
        break;

    case people.names[2]:
        alert(people.names[2] + "'s email: " + people.emails[2] + " phone number: " + people.phonenums[2]);
        break;

    default:
        alert("I don't know that person.");
}

In your particular scenario, you may be better off searching for the index of the correct person and using that so your array is free to grow and shrink. Something like this could work:

var search = prompt("Type in someone's name to find their phone number and email.");
boolean found = false;
int i = 0;
while(!found && i<people.names.length) {
    if(people.names[0] == search){
        found=true;
    } else {
        i++;
    }
}
if(found){
        alert(people.names[i] + "'s email: " + people.emails[i] + " phone number: " + people.phonenums[i]);
} else {
        alert("I don't know that person.");
}

My js is rusty and I'm using my phone, I'll check later for syntax errors if no one else spots any.

CasualViking
  • 262
  • 1
  • 9
  • Well that was stupid. I had a feeling I was forgetting something. Thanks. – theratkeeper May 30 '19 at 20:02
  • Easily done. By the way, having another look at your code, you may want to take a different approach. As it is right now, you need to hard code each number that could come up. If you instead searched the array (while loop could work simply enough) and then used the index at which the person was found then your array would be free to grow or shrink. – CasualViking May 30 '19 at 20:07
  • That was actually exactly what I was thinking to do next :) – theratkeeper May 30 '19 at 20:11
  • I've just popped a possible code for that in the answer you could try if you want. – CasualViking May 30 '19 at 20:15
  • Thanks for that. I will most likely use it. – theratkeeper May 30 '19 at 20:38
2

You need to provide a break statement

wosi
  • 373
  • 4
  • 15