Conditional (ternary) operator in JavaScript Course

Hi, in this task I should refactor the first if...else block to use a ternary operator.

if (isLocked) {
  console.log('You will need a key to open the door.');
} else {
  console.log('You will not need a key to open the door.');
}

my solution is:

console.log(isLocked? 'You will need a key to open the door.': 'You will not need a key to open the door.');

Why it is not ok?

You have just typed a string as the thing that happens if it is true or false. You have not done anything with the string (like log it to the console.).

1 Like

I tried it, and the code seems to run.

let isLocked = true; console.log(isLocked ? 'You will need a key to open the door.' : 'You will not need a key to open the door.' );

Maybe the exercise wants you to do it in a more specific way?

let isLocked = true;
let needKey = isLocked ? "You will need a key to open the door." : "You will not need a key to open the door.";
console.log(needKey);
1 Like

Thanks. You are right. Maybe the exercise wants different way or/and the “checker” is not perfect to predict all possible ways.