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).

@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.