How to convert if to switch statement in JavaScript? (beginner JavaScript)
Go to solution
Solved by RockSolid1106,
2 hours ago, c0d0ps said:
Console log will log the text to the *console*. What you want to do is set the value of the paragraph element, for which variables have already been defined near the end of the code. All you need to do is set the value of the variable "response" to the text.
console.log("That's a great score, you really know your stuff.";); // to response = "That's a great score, you really know your stuff.";
2 hours ago, c0d0ps said:case score >= 70 && score < 89: console.log("That's a great score, you really know your stuff."); break;
break should also have the same indentation as the rest of the block in the case.
Corrected:
2 hours ago, c0d0ps said:case score >= 70 && score < 89: console.log("That's a great score, you really know your stuff."); break;
After all the changes, your code should look like this:
let response; let score = 75; let machineActive = true; switch (machineActive) { case score <= 0 || score >= 100: response = "This is not possible, an error has occurred."; break; case score >= 0 && score <= 19: response = 'That was a terrible score — total fail!'; break; case score >= 20 && score < 39: response = 'You did a passable job, not bad!'; break; case score >= 40 && score < 69: response = 'You did a passable job, not bad!'; break; case score >= 70 && score < 89: response = "That's a great score, you really know your stuff."; break; case score >= 90 && score < 100: response = 'What an amazing score! Did you cheat? Are you for real?'; break; // include as many cases as you like default: response = 'The machine is turned off. Turn it on to process your score.';} // Don't edit the code below here! section.innerHTML = ' '; let para1 = document.createElement('p'); let para2 = document.createElement('p'); para1.textContent = `Your score is ${ score }.`; para2.textContent = response; section.appendChild(para1); section.appendChild(para2);
Spoiler
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now