Assessment wanted for Conditionals 1-4 skill test #learning

These are assignments:

This solution:

  1. Conditionals 1 - JSFiddle - Code Playground
  2. Conditionals 2 - JSFiddle - Code Playground
  3. Conditionals 3 - JSFiddle - Code Playground
  4. Conditionals 4 - JSFiddle - Code Playground

Hi Svitcher and welcome back! :wave:

All four tasks work as expected. Congratulations! :tada:
Here are some remarks regarding readability/simplicity:

  • Task 2: For better readability I recommend writing the code inside the if statement on a new line:
    if (score < 0 || score > 100) {
      response = 'This is not possible, an error has occurred.';
    } else if ...
    
  • Task 2 + 3: When testing if a boolean is false it’s simpler to write if (!machineActive) instead of if ( machineActive === false )
  • Task 4: You could simplify the ternary operator by putting pwdResult in front:
    pwdResult = pwd === 'cheese' ? 'Login successful.' : 'Password incorrect; login failed.'
    

I hope my feedback is helpful. Please just ask if you have more questions or tasks to be assessed :blush:

Michael

Hello! Thank you so much for your advice. They are very important to me. I tried this code in the 4th task:
'pwdResult = pwd === ‘cheese’ ? ‘Login successful.’ : ‘Password incorrect; login failed.’
But the program gives the result: true and doesn’t assign the right value to the pwdResult variable. :wink:
Nataliia

1 Like

Hmm… I just copied my code into your codepen (replacing your own line) and it worked :thinking: It looks like this:

if (machineActive) {
  machineResult = 'The machine is switched on';
  pwdResult = pwd === 'cheese' ? 'Login successful.' : 'Password incorrect; login failed.'
} else {
  machineResult = 'The machine is switched off';
}

This is a new link: Conditionals 4 - JSFiddle - Code Playground
My expression was in brackets. Yes, JS has a strange syntax. :slightly_smiling_face:

1 Like

Ahhhhh… now I see :slightly_smiling_face:

(pwdResult = pwd === 'cheese') ? 'str1': 'str2' /* pwd === 'cheese' gets evaluated first */
(pwdResult = true) ? 'str1': 'str2' /* true gets assigned to pwdResult */
true ? 'str1': 'str2' /* evaluates to 'str1', but it doesn't get assigned anywhere */

It’s sometimes tricky to know what gets evaluated/assigned first. This topic is called “operator precedence”. If you are brave enough MDN has an article about it: MDN | Operator Precedence. Especially the table at the end is pretty scary :wink:

I got it, thank you. :hugs:

1 Like