let question = (‘What is the error in my code for switch statements?’)
console.log(`New to Codecademy and made it to the magic 8 ball lesson in the tutorial, I ran into a problem… the switch statment seems to give me a syntax issue. My question ${question}. Your help is apprecaiated.)
Thank you!
-Bello
I think I solved it!!!
Basically I didnt assign the following let eightBall = randomNumber!
Feel free to let me know how to clean it up or improve please!
Have another look at how switch is used.
switch(randomNumber) {
case 1:
console.log("It is certain");
break;
case 2:
console.log("It is decidedly so");
break;
default:
console.log("No way!");
}
notice that I used
case 1:
but not
case randomNumber = 1:
and not
case randomNumber === 1:
The previous code is mostly equivalent to doing
if (randomNumber === 1) {
console.log("It is certain");
}
else if (randomNumber === 2) {
console.log("It is decidedly so");
}
else {
console.log("No way!");
}
Another issue is that you used
switch (eightBall) {
but the number you are using to decide which case to run is randomNumber
so that could be
switch (randomNumber) {
instead.
If the instructions say to change the value of eightBall
to be a string that’s different for each case, then you should change the value of eightball
rather than do console.log
in each case. But I don’t remember whether the instructions say to do this.
let eightBall = "";
switch(randomNumber) {
case 1:
eightBall = "It is certain";
break;
case 2:
eightBall = "It is decidedly so";
break;
default:
eightBall = "No way!";
}
Check whether you can get something other than only “It is certain” when you run your code multiple times.
1 Like
I think I am following,
switch values take the condition we have stated as true…
So since I previously used:
[codebyte]
let eightBall = randomNumber
For the switch cases I only need to focus on expressing the potential scenarios of the randomNumber variable. since any of those cases will be equal to the conditon of eightBall.
*wags
-Bello
This is correct I believe!