I need help please

//switch final position to get different answers

let runnerFinalPosition = 'fourth place'

if (runnerFinalPosition === 'first place') {console.log('You were the fastest runner!')}

else {console.log('Sorry, you were not the fastest runner.')};

switch (runnerFinalPosition) {case 'first place': console.log('You\'ve won GOLD!')

break;

case 'second place': console.log('You\'ve won SILVER!')

break;

case 'third place': console.log('You\'ve won BRONZE!')

break;

default: console.log('You have not won a medal.')

break;

}

if (runnerFinalPosition === 'first place' || 'second place' || 'third place') {console.log('This means you\'ve placed in the final top three!')}

else {console.log('You have not placed in the final top three.')};

The last else statement does not execute when I change the runnerFinalPosition to anything other than 1st, 2nd, or 3rd place. I’m a beginner so I don’t know if I’m missing something or if I typed something in wrong. I would appreciate some help and any tips as well! Thanks!

If the way it’s formatted in this post is the way you’ve actually written it, then you’ll need to fix that.

Before:

//switch final position to get different answers

let runnerFinalPosition = 'fourth place'

if (runnerFinalPosition === 'first place') {console.log('You were the fastest runner!')}

else {console.log('Sorry, you were not the fastest runner.')};

switch (runnerFinalPosition) {case 'first place': console.log('You\'ve won GOLD!')

break;

case 'second place': console.log('You\'ve won SILVER!')

break;

case 'third place': console.log('You\'ve won BRONZE!')

break;

default: console.log('You have not won a medal.')

break;

}

if (runnerFinalPosition === 'first place' || 'second place' || 'third place') {console.log('This means you\'ve placed in the final top three!')}

else {console.log('You have not placed in the final top three.')};

after:

//switch final position to get different answers
let runnerFinalPosition = 'fourth place'

if (runnerFinalPosition === 'first place') {
	console.log('You were the fastest runner!')
}

else {console.log('Sorry, you were not the fastest runner.')};

switch (runnerFinalPosition) {
	case 'first place':
		console.log('You\'ve won GOLD!')
		break;
	case 'second place':
		console.log('You\'ve won SILVER!')
		break;
	case 'third place':
		console.log('You\'ve won BRONZE!')
		break;
	default:
		console.log('You have not won a medal.')
		break;
}

if (runnerFinalPosition === 'first place' || 'second place' || 'third place') {
	console.log('This means you\'ve placed in the final top three!')
}

else {
	console.log('You have not placed in the final top three.')
};

This will allow everything to run as coded, but you should also make sure you add semicolons at the end of lines where they are omitted (after let... and each console.log()).

The reason the else is not executing is because your if statement always evaluates to true.

That’s because when runnerFinalPosition is not equal to the value you’re comparing it to, it is returning the next value, because you’ve formatted it with double pipes (||), which is a logic expression.

Basically…

let runnerFinalPosition = "x";
console.log(runnerFinalPosition === "x" || "y");

“x”

and

let runnerFinalPosition = "a";
console.log(runnerFinalPosition === "x" || "y");

“y”

because the bit after || is returned if the bit before it is false.

Since you’re either returning true (if runnerFinalPosition is equal to the string you compare it against) or a string (if it’s false), the if statement will always evaluate to true. That’s simply because a string with content is equal to true, while an empty string is false:

image


Therefore, in order to fix this, you need to switch from using the || operator like this:

if (x === "abc" || "xyz")

to using it like this:

if (x === "abc" || x === "xyz")

…does that make sense?

I’m deliberately not giving you the full answer because I think you’ll be better off if you figure it out yourself, based on what I’ve said here. If you understand it, rather than just knowing it, you’ll be able to debug better going fowards.

Let me know if you have any further questions!

2 Likes

Hi, I just started learning 2 days ago so I don’t really understand what you said in this section. However, I was able to fix the code based on what you were saying about the || operator.

It now looks like this:

//switch final position to get different answers
let runnerFinalPosition = 'fourth place';

if (runnerFinalPosition === 'first place') {
  console.log('You were the fastest runner!');
  }
else {console.log('Sorry, you were not the fastest runner.')
}

if (runnerFinalPosition === 'first place' || 
    runnerFinalPosition === 'second place' ||
    runnerFinalPosition === 'third place') 
 {console.log('This means you\'ve placed in the final top three!')
 }

else {
  console.log('You did not place in the final top three.')
  };

switch (runnerFinalPosition) {
  case 'first place': 
console.log('You\'ve won GOLD!');
break;
case 'second place': console.log('You\'ve won SILVER!')
break;
case 'third place': 
console.log('You\'ve won BRONZE!');
break;
default: 
console.log('So, you have not won a medal.');
break;
}

Hopefully that’s right. Is there a reason it should look like this though? Is the other way wrong?

Also, thanks for the help! it’s much appreciated!

Basically:

(expression1) || (expression2) || ...

will check if the first expression is true. If it is, true is returned and we look no further.

If the first expression is false, we check the second and if this is true, we return that, and so on.

If none of the expressions are true, false is returned. If any are, then true is returned, or the value of the string when it’s a string:

false || (1+1)===3 || "hello"

“hello”

true || "hello"

true

When a string with content is returned (as opposed to an empty string - ""), this will evaluate to true. Empty strings return false.

Therefore:

let runnerFinalPosition = "third place";
runnerFinalPosition === "first place" || "second place" || "third place";

“second place”

because runnerFinalPosition === "first place" is false and the first value equivalent to true in the line is "second place" - a string with content.

1 Like

Okay, that makes sense! What about the formatting before? Does that actually matter, as in affect the way the code is run?

In this case, no.

In general, maybe.

You had this part in one line:

if (runnerFinalPosition === 'first place') {console.log('You were the fastest runner!')}

The problem you could face here is that if you added something else after the console.log(), you’d need a semicolon:

console.log("one ") console.log("two")

Uncaught SyntaxError: Unexpected identifier

That means “where did the second c come from? We were expecting a semicolon”.

console.log("one "); console.log("two")

one
two

As long as you have semicolons between each, you’ll be fine. You can leave the semicolon off the last one, as shown above.

It’s just a good habit to get into.


Another thing to note is that it generally becomes harder to read code that’s all in line. That’s why I broke your cases up. Try to keep case x: and break; on separate lines to everything else, so you can clearly see where it starts and finishes.

1 Like