Is there a simpler way to assign a variable based on a conditional statement

Hi!

I’m working my way through the Learn JavaScript course. I just finished the Magic Eightball Project and I have a question about conditionals and assignment.

As the last step of the project, I need to assign the variable eightBall to one of eight different strings, based on the value of the randomNumber variable. My code ended up looking like this:

switch (randomNumber) {
  case 0 : eightBall = 'It is certain';
  break;
  case 1 : eightBall = 'It is decidely so';
  break;
  case 2 : eightBall = 'Reply hazy try again';
  break;
  case 3 : eightBall = 'Cannot predict now';
  break;
  case 4 : eightBall = 'Do not count on it';
  break;
  case 5 : eightBall = 'My sources say no';
  break;
  case 6 : eightBall = 'Outlook not so good';
  break;
  case 7 : eightBall = 'Signs point to yes';
  break;
 };

That works, and I believe it’s what was expected, but it felt less than optimal to repeat the assignment operation in each clause of the switch statement. I would have much rather had just one assignment, where the variable is set to a value computed by the conditional clause. That would express my intent more clearly. Something like this.

eightball = {switch (randomNumber) {
 case 0 : 'It is certain';
 break;
 case 1 : 'It is decidely so';
 break;
 case 2 : 'Reply hazy try again';
 break;
 case 3 : 'Cannot predict now';
 break;
 case 4 : 'Do not count on it';
 break;
 case 5 : 'My sources say no';
 break;
 case 6 : 'Outlook not so good';
 break;
 case 7 : 'Signs point to yes';
 break;
};
};

I understand why this doesn’t work – switch is a statement, not an expression, so it doesn’t evaluate to a value. What I’m asking is whether there is some other way in JavaScript to refactor the code and achieve the same result: writing the assignment operator only once, and then putting the conditional on the right hand side of that assignment.

Thanks in advance for any insights you can provide.

Hi,

One thing you could use is an array to generate the statement.
For example:

let statements = ["It is certain", "It is decidely so"] //Repeat including all statements
let eightBall = statements[randomNumber];

This is a lot simpler than the switch statement you are using.

Hope that helps!

3 Likes

You beat me to it. :smiley:

1 Like

Good question, and good answer.

You’ll find there are multiple techniques that can all get the desired outcome, and the curriculum starts with the basics (easiest to conceptualise) and then introduces you to more as your understanding grows. This particular lesson wanted to teach you about switch statements, even if it’s not the most efficient use of code for a magic 8 ball.

3 Likes

Thanks, very interesting. I see how that works! It wouldn’t have occurred to me to create an array to do this. I wonder if that would have an efficiency hit.

I moved on to the next lesson in the course – functions – and now have another way to write it. the return keyword lets me return a value from inside a switch statement. With a return I can skip the break. That leads to this code:

let eightBall = ''

function numberToMessage (number) {
  switch (number) {
 case 0 : return 'It is certain';
 case 1 : return 'It is decidedly so';
 case 2 : return 'Reply hazy try again';
 case 3 : return 'Cannot predict now';
 case 4 : return 'Do not count on it';
 case 5 : return 'My sources say no';
 case 6 : return 'Outlook not so good';
 case 7 : return 'Signs point to yes';
 };
}

eightBall = numberToMessage(6)
console.log(eightBall)

I’d be interested in what people think of that from a style standpoint. Is that a reasonable approach?

1 Like