10 hands-on exercises and 10 find-the-bug challenges for the JavaScript if else Statement lesson. Every one is checked automatically in the browser — write the code, run it, and the tests tell you what still fails.
Given the score stored in `score`, write an if/else if/else chain that sets #result to "A" (90+), "B" (80-89), or "C" (below 80). With score = 87, the result should read "B".
Given the number stored in `number`, set #result to "Even" if it divides evenly by 2, otherwise "Odd". With number = 17, the result should read "Odd".
Given the role stored in `role`, if it equals "admin" set #status to "Access granted" and add the class "granted" to #status; otherwise set it to "Access denied" and add the class "denied". With role = "admin", access should be granted.
Given the temperature (in Celsius) stored in `temperature`, set #advice to "Wear a coat" below 10, "Wear a light jacket" from 10 up to (but not including) 25, or "Wear a t-shirt" at 25 and above. With temperature = 15, the advice should be "Wear a light jacket".
Given the cart total stored in `cartTotal`, if it's $100 or more apply a 10% discount and show the new total in #total as "Total: $108"; otherwise show the total unchanged. With cartTotal = 120, the discounted total is 108.
Show "pass" when the score is 70 or more, otherwise "fail". Score is 82.
Show "on" or "off" for a switch that is currently false.
Show the larger of 14 and 9.
Show "even" or "odd" for the number 8.
Rewrite a simple branch as one line: 5 is less than 10, so show "small".
This access check always grants access, no matter the role, because the condition uses = (assignment) instead of === (comparison). Fix the operator so role = "guest" is correctly denied.
This should recommend a coat below 10°C, but the comparison is backwards, so it recommends a coat above 10°C instead. Fix the operator. With temperature = 4, a coat should be recommended.
Ready to write the code?
The editor, the automatic checks and the worked solutions open up with an account.
Open the workspaceOrders of exactly $100 should qualify for the 10% discount, but the condition uses > instead of >=, so a $100 order is excluded. Fix the operator. With cartTotal = 100, the total should become $90.
This checker has its labels swapped: it says "Even" when the remainder is non-zero. Fix the logic so odd numbers are correctly labeled "Odd". With number = 17, the result should read "Odd".
This grade calculator checks the 80+ range before the 90+ range, so a 95 incorrectly gets graded "B". Reorder the checks from highest to lowest so 95 correctly gets "A".
The two outcomes are the wrong way round.
A score of exactly 70 should pass, but it fails.
The remainder test is comparing against the wrong value.
The ! makes the wrong branch run.
The two ternary outcomes are reversed.