1

I would like some advice on how to slim down this switch:

switch (lotUser | winningLot) {
        case lotUser === winningLot[0]:
        case lotUser === winningLot[1]:
        case lotUser === winningLot[2]:
        case lotUser === winningLot[3]:
        case lotUser === winningLot[4]:
        case lotUser === winningLot[5]:
        case lotUser === winningLot[6]:
        case lotUser === winningLot[7]:
            console.log("You win!");
            break;
        default:
            console.log("You do not win!");
            break;
    }

Instead of

case lotUser === winningLot[0]:

I wrote the script to be:

switch (lotUser | winnendLot) {
    case lotUser === winnendLot[0|1|2|3|4|5|6|7]:
        console.log("You win!");
        break;
    default:
        console.log("You do not win!");
        break;
}

I just don't know if this works the way I want it to work. It needs to check if the generated lotUser is equal to one of the values in an array (winningLot). If the lotUser equals one or more of the values in the winningLot array, it should output "You win!". Could someone please confirm that my code does the description I gave?

5
  • This does not do what you want it to do. The first script is closer to the correct way to use the switch. It is verbose, but it is the correct way. Also, you are misusing the | character. It is a bitwise or operation. So you are actually doing some math between the operators, not choosing one or the other developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented Oct 24, 2013 at 17:19
  • 4
    You seam to misunderstand the '|' operator. This is a binary OR operator. You will never need this in your code unless you know that you do. Commented Oct 24, 2013 at 17:20
  • @PatrickGunderson: How would you suggest making it slim and bite size? Commented Oct 24, 2013 at 17:21
  • | is binary OR. 0|1|2|3|4|5|6|7 will always be equal to 7 so that there is your first issue. Commented Oct 24, 2013 at 17:21
  • @JohannBehrens Your first code is correct (notwithstanding the | in the first line). It doesn't get more bite size than that. Commented Oct 24, 2013 at 17:22

3 Answers 3

5

What about Array.prototype.indexOf()?

if (winnedLot.indexOf(lotUser) !== -1) {
  console.log("Won!");
}
else {
  console.log("Lost!");
}

It searches the array for the first occurrence of the value stored in lotUser and returns its respective index.

Since you do not need to count the occurrences, this should be the best way.


If you want to count them, use a loop:

var count = 0;

for (var i=0, len=winnedLot.length; i<len; i++) {
  if (winnedLot[i] === lotUser) {
    count++;
  }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Read into the indexOf() and it is completely clear to me! Thank you for answering :)
As an additional question, what if I want to log something specific to one position of the array?
@JohannBehrens Use the second code and just insert console.log() or whatever you like in the if statement.
2

You can simply use indexOf:

if(winningLot.indexOf(lotUser) >= 0) {
    ...
} else {
    ...
}

Comments

1

Well for starters you're using switch incorrectly. The value to compare goes in the switch(...) part, and the possible values are listed by each case ...:

Anyway, that aside, all you want is to check if lotUser is in the winnendLot array. Easy:

// assuming supporting browser:
if( winnendLot.indexOf(lotUser) > -1) console.log("You win!");

// full browser support:
var winner = false, l = winnendLot.length, i;
for( i=0; i<l; i++) {
    if( winnendLot[i] === lotUser) {
        winner = true;
        break;
    }
}
if( winner) console.log("You won!");

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.