JavaScript if else Statement — exercises and bug fixes
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.
Exercises(10)
- 1
Grade a score
easy15 minGiven 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".
- 2
Check even or odd
easy15 minGiven 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".
- 3
Check access by role
easy15 minGiven 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.
- #status should read "Access granted".
- #status should have the class "granted".
- 4
Suggest clothing for the weather
easy15 minGiven 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".
- 5
Apply a bulk discount
easy15 minGiven 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.
- 6
Branch on a number
easy10 minShow "pass" when the score is 70 or more, otherwise "fail". Score is 82.
- 7
Branch on a boolean
easy10 minShow "on" or "off" for a switch that is currently false.
- 8
Pick the larger number
easy10 minShow the larger of 14 and 9.
- 9
Detect an even number
easy10 minShow "even" or "odd" for the number 8.
- 10
Use a ternary instead
medium10 minRewrite a simple branch as one line: 5 is less than 10, so show "small".
Find the bug(10)
- 1
Fix the assignment instead of comparison
easy15 minThis 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.
- With role = "guest", #status should read "Access denied".
- #status should have the class "denied".
- 2
Fix the backwards temperature check
easy15 minThis 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.
- 3
Fix the discount boundary
easy15 minOrders 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.
- 4
Fix the swapped even/odd labels
easy15 minThis 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".
- 5
Fix the grade chain's check order
easy15 minThis 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".
- 6
Fix the swapped branches
easy8 minThe two outcomes are the wrong way round.
- 7
Fix the off-by-one boundary
easy8 minA score of exactly 70 should pass, but it fails.
- 8
Fix the even check
easy8 minThe remainder test is comparing against the wrong value.
- 9
Fix the negated boolean
easy8 minThe ! makes the wrong branch run.
- 10
Fix the ternary order
easy8 minThe two ternary outcomes are reversed.
Ready to write the code?
The editor, the automatic checks and the worked solutions open up with an account.
Open the workspace