Assessment wanted Conditionals 3

Hello, I tried solving Conditionals 3, didn’t have any trouble with 2 or 1, but no matter what i do with 3 i always get the default message.

Here is the Test Your Skills page

and here is my solution

After searching for a bit I have found the issue.
It has to be
switch (true) instead of switch (score).

2 Likes

@Ceeroo that’s interesting; our final solution uses switch(score), but it’d be interesting to see what form your solution takes. There is usually more than one solution to any given problem.

@chrisdavidmills When I copy your final solution it doesn’t work. It returns error.Annotation 2020-03-09 170213

Argh, you are right :wink:

So my publishing solution was wrong - it should have used switch (true) and NOT switch (score)! So @Ceeroo was right all along.

I have corrected my solution.

I’ve had the same issue but after writing switch(true) instead of switch(score) it ran fine. However, do you know what the reason is to do switch(true)?

As far as I understand it, when you use a switch statement, the variable inside the parentheses after switch is the thing you are testing the value of, then the values inside the parentheses after the case statements are the different values you are testing for.

In a really simple case such as this:

switch (happy) {
  case (true):
    response = 'I am happy';
    break;
  case (false):
    response = 'I am sad';
    break;
}	

This works fine, as here we are testing whether happy is equal to true or false, and then doing something as a result.

In more complex cases, like in this assessment, this kind of structure doesn’t work.

If our value to test is set as score, how do we then write the condition of score >= 0 && score < 20 without mentioning score? Instead, we write true as a kind of dummy test value, then write the entire test inside the cases, including the value to test.

We could do the same thing with our happy/sad example like this:

switch (true) {
  case (happy === true):
    response = 'I am happy';
    break;
  case (happy === false):
    response = 'I am sad';
    break;
}	

or

switch (true) {
  case (happy):
    response = 'I am happy';
    break;
  case (!happy):
    response = 'I am sad';
    break;
}	

But this is not necessary in this simple case.

2 Likes

Hi Chrisdavidmills,

Can you please help me understand why is this not working? I’ve purposely jot down half of it to keep it short.

Thanks in advance,

Because you left the function machineActive = false. It should be set to true.

Hi, I had the same problem and switched to true, but also was using single quotes instead of parentheses after case. I was wondering why quotes were used after case in the lesson example, and should you always use () ?

Hi @nickmaskell

It depends what the type of the value is. If you have string as case you would use quotes. For a number nothing. For a logical expression like here that either returns true or false it makes sense to group it with parentheses. (It may also work without, I’m not sure.)

Michael