docs.rodeo

MDN Web Docs mirror

Test your skills: Conditionals

The aim of this skill test is to assess whether you’ve understood our Making decisions in your code — conditionals article.

[!NOTE] You can try solutions by downloading the code and putting it in an online editor such as CodePen, JSFiddle, or Glitch. If there is an error, it will be logged in the results panel on the page or into the browser’s JavaScript console to help you.

If you get stuck, you can reach out to us in one of our communication channels.

Conditionals 1

In this task you are provided with two variables:

We want you to create a conditional that checks whether season contains the string “summer”, and if so assigns a string to response that gives the user an appropriate message about the season. If not, it should assign a generic string to response that tells the user we don’t know what season it is.

To finish off, you should then add another test that checks whether season contains the string “winter”, and again assigns an appropriate string to response.

Try updating the live code below to recreate the finished example:

{{EmbedGHLiveSample("learning-area/javascript/building-blocks/tasks/conditionals/conditionals1.html", '100%', 400)}} 

[!CALLOUT]

Download the starting point for this task to work in your own editor or in an online editor.

Conditionals 2

For this task you are given three variables:

You need to create an if...else structure that checks whether the machine is switched on and puts a message into the response variable if it isn’t, telling the user to switch the machine on.

Inside the first if...else, you need to nest another if...else that puts appropriate messages into the response variable depending on what the value of score is — if the machine is turned on. The different conditional tests (and resulting responses) are as follows:

Try updating the live code below to recreate the finished example. After you’ve entered your code, try changing machineActive to true, to see if it works. Please note that, for the scope of this exercise, the Your score is __ statement will remain on the screen regardless of the machineActive’s value.

{{EmbedGHLiveSample("learning-area/javascript/building-blocks/tasks/conditionals/conditionals2.html", '100%', 400)}} 

[!CALLOUT]

Download the starting point for this task to work in your own editor or in an online editor.

Conditionals 3

For the final task you are given four variables:

We’d like you to create an if...else structure that checks whether the machine is switched on and puts a message into the machineResult variable telling the user whether it is on or off.

If the machine is on, we also want a second conditional to run that checks whether the pwd is equal to cheese. If so, it should assign a string to pwdResult telling the user they logged in successfully. If not, it should assign a different string to pwdResult telling the user their login attempt was not successful. We’d like you to do this in a single line, using something that isn’t an if...else structure.

Try updating the live code below to recreate the finished example:

{{EmbedGHLiveSample("learning-area/javascript/building-blocks/tasks/conditionals/conditionals3.html", '100%', 400)}} 

[!CALLOUT]

Download the starting point for this task to work in your own editor or in an online editor.

In this article

View on MDN