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".
Acceptance criteria
- 1.#result should read "B" for a score of 87.
JavaScript Tutorial · JavaScript Conditions
These 20 tasks go with the JavaScript if else Statement chapter. 10 of them ask you to build something small from scratch; 10 give you code that is already broken and ask you to work out why. Set aside about 240 minutes for the whole set, though you can do them in any order.
Nothing here is marked by a person. When you press Submit, the editor checks your code against the points listed under each task and tells you straight away what passed and what didn't (you need a free account to submit). If you get stuck, there are hints, and a worked solution once you've had a go. The first 2 tasks are free; the rest are part of the practice plan.
If it's been a while since you read the chapter, here is how it starts: “The JavaScript if else statement is used when you want to run one block of code if a condition is true, and another block of code if the condition is false.” .
1.Grade a scoreexercise · easy · 15 min
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".
Passes when: #result should read "B" for a score of 87.
2.Check even or oddexercise · easy · 15 min · practice plan
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".
Passes when: #result should read "Odd" for 17.
3.Check access by roleexercise · easy · 15 min · practice plan
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.
Passes when: #status should read "Access granted"; #status should have the class "granted".
4.Suggest clothing for the weatherexercise · easy · 15 min · practice plan
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".
Passes when: #advice should read "Wear a light jacket" for 15°C.
5.Apply a bulk discountexercise · easy · 15 min · practice plan
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.
Passes when: #total should read "Total: $108".
6.Fix the assignment instead of comparisonbug fix · easy · 15 min
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.
Passes when: With role = "guest", #status should read "Access denied"; #status should have the class "denied".
7.Fix the backwards temperature checkbug fix · easy · 15 min · practice plan
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.
Passes when: With temperature = 4, #advice should read "Wear a coat".
8.Fix the discount boundarybug fix · easy · 15 min · practice plan
Orders 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.
Passes when: With cartTotal = 100, #total should read "Total: $90".
9.Fix the swapped even/odd labelsbug fix · easy · 15 min · practice plan
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".
Passes when: With number = 17, #result should read "Odd".
10.Fix the grade chain's check orderbug fix · easy · 15 min · practice plan
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".
Passes when: With score = 95, #result should read "A".
11.Branch on a numberexercise · easy · 10 min · practice plan
Show "pass" when the score is 70 or more, otherwise "fail". Score is 82.
Passes when: #out should show pass.
12.Branch on a booleanexercise · easy · 10 min · practice plan
Show "on" or "off" for a switch that is currently false.
Passes when: #out should show off.
13.Pick the larger numberexercise · easy · 10 min · practice plan
Show the larger of 14 and 9.
Passes when: #out should show 14.
14.Detect an even numberexercise · easy · 10 min · practice plan
Show "even" or "odd" for the number 8.
Passes when: #out should show even.
15.Use a ternary insteadexercise · medium · 10 min · practice plan
Rewrite a simple branch as one line: 5 is less than 10, so show "small".
Passes when: #out should show small.
16.Fix the swapped branchesbug fix · easy · 8 min · practice plan
The two outcomes are the wrong way round.
Passes when: #out should show pass.
17.Fix the off-by-one boundarybug fix · easy · 8 min · practice plan
A score of exactly 70 should pass, but it fails.
Passes when: #out should show pass.
18.Fix the even checkbug fix · easy · 8 min · practice plan
The remainder test is comparing against the wrong value.
Passes when: #out should show even.
19.Fix the negated booleanbug fix · easy · 8 min · practice plan
The ! makes the wrong branch run.
Passes when: #out should show off.
20.Fix the ternary orderbug fix · easy · 8 min · practice plan
The two ternary outcomes are reversed.
Passes when: #out should show small.
Between them these tasks cover JavaScript if else Statement, if/else branching and Boolean logic.
Looking for a different chapter? See every practice set, or switch to the interview theory questions.