Javascript fundamentals - assignment 6

I’ve seen one other thread about this assignment here, but that user went about it a different way.

The assignment is:

A Snarky Robot

Write code that asks the user what their favorite number is and then responds according to the following guidelines:

  1. If the number is 42, the response should be “Yay! That’s my favorite too!”

  2. If the number is less than 42 the response should be: “Eh, that’s OK but X would be even better” for this response X should be the number that they guessed + 1

  3. If the number is higher than 42 the response should be: “LAME. That number is too large!”

Here’s what I have so far:

let number = prompt('what is your favorite number');
let response;

switch (number) {
  case number == 42:
    response = "Yay! That's my favorite too!";
  case number < 42:
    response = "Eh, that's okay but " + (number + 1) + "would be even better";
  case number > 42:
    response = "LAME. That number is too large!";


When I enter any number into the prompt, I get “undefined” in the console.

What might be causing this?

1 Like

I don’t think you can use a switch statement here. Switch statements match on a strict === basis which means the input must be the same as the matching case statement. In your case I believe it is doing "42 === true for example which never passes.

Thank you @CouchofTomato. I switched it to if, else if, else and got it working.

Can a switch statement really not work in this circumstance? Even some of the examples in the instructions look very similar and don’t look like they use === when comparing inputs.

This code is from the W3Schools page on using switch:

  case 0:
    day = "Sunday";
  case 1:
    day = "Monday";
  case 2:
     day = "Tuesday";
  case 3:
    day = "Wednesday";
  case 4:
    day = "Thursday";
  case 5:
    day = "Friday";
  case 6:
    day = "Saturday";

Why couldn’t this be used similar to complete the assignment 6 exercise?

Javascript uses === under the hood to compare. So in the example above that when it receives an input in the switch such as 1, it will go down the case statements comparing each like 1 === 0 and then 1 === 1. Once it finds a match it will evaluate the code within that statement.

In your switch statement was on number, but the case statement was number == 42 or number < 42 etc which returns true or false. So in your example say the number was 45. It was evaluating it like 45 === false, 45 === false and then 45 === true. === is strict equality so each side of the evaluation must hold the same data type and value.

Don’t have much time but for one, you should probably use if/elseif/else since it’ll be easier. Secondly mind how you have (number + 1). That will not work and result in whatever you enter having 1 attached to the side of it. So say you enter 30 instead of making it 31 it’ll be 301. To solve this apply the Number function which converts number(variable) to a number(data type). The syntax will be (Number(number) + 1) or something along those lines.

Thanks for the insight! Seems to work when the syntax is also (Number(number) + Number(1)).

the input you get from prompt(); is of string type
Please convert it to number type using parseInt();
replace with : let number = parseInt(prompt(‘what is your favourite number’));

Here is what i have and it works perfectly!

number = prompt('what is your favorite number')
var num= parseInt(number)
let x = (num + 1)
if (number==42) {response=("Yay! That's my favorite too!")} else if (number<42) {response=("Eh, that's OK but " + x + " would be even better")} else if (number>42) {response=("LAME. That number is too large!")}