Continuing my journey through JavaScript I have just completed the tests under Conditionals. I would really appreciate any feedback for the logic/methods I used.
Conditionals 1:
I was a bit confused by the prompt - I didn’t know if I was supposed to use If…else + another completely different paragraph of code (another If) OR just use If…else if. I solved it both ways but have just commented out one of the solutions.
Task 1: Correct Something like your commented out code was requested. It’s just a basic test to see if you understand nested if..else statements. (That you obviously do )
Task 2: Nearly correct The first conditional should have < and > because 0 and 100 are valid scores. Simplification: Testing a boolean for true is redundant. You can just write if (machineActive) for the same effect.
Task 3: Correct You could also put the first condition into a default option at the end of the switch statement.
Task 4: Correct Simplification: Put pwdResult = at the start of the line to not having to repeat it.
pwdResult = (pwd === 'cheese') ? "..." : "...";
On a general note: When using CodePen you should put your CSS and JS in there respective panes instead of using the given <style> and <script> tags. It improves readability.
@mikoMK thank you for all the tips and for taking the time to evaluate my tasks!
The simplification you suggested for Task 4 was really helpful. Did not cross my mind that I could do that, so it’s nice that I can get this piece of info under my belt.
Also I’ll definitely include the JS in the appropriate pane in further pens!
I’m glad you learned something new
The reason this works is because the ternary has a higher precedence (priority) than the assignment. If you want to scare yourself have a look at the table in the link.
We there see that the equality has 9, ternary has 3 and assignment has 2.
Heheh. I did get to take a peek at operator precedence in a previous lesson (I’m doing The Odin Project) but it’s definitely a lot to take in. I’m guessing I’ll get more and more used to them as I actually put them into practice.
I have never heard of it. Thanks for mentioning it. Looks interesting and will have a look.
Exactly! You will probably never encounter most of the possible combinations of this table. Others just feel natural like new or + having a higher precedence than =. Personally, I never went to this table and tried to memorize it. (and I definitely couldn’t rewrite it off the top of my head)