If age is 20, show "adult".
Acceptance criteria
- 1.#out should show adult.
JavaScript Tutorial · JavaScript Conditions
These 10 tasks go with the JavaScript if Statement chapter. 5 of them ask you to build something small from scratch; 5 give you code that is already broken and ask you to work out why. Set aside about 90 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 statement is used to run a block of code only when a condition is true.” .
1.Show a message when trueexercise · easy · 10 min
If age is 20, show "adult".
Passes when: #out should show adult.
2.Test a string valueexercise · easy · 10 min · practice plan
If the role is "admin", show "allowed".
Passes when: #out should show allowed.
3.Check an empty listexercise · easy · 10 min · practice plan
If the cart has no items, show "empty".
Passes when: #out should show empty.
4.Combine two conditionsexercise · easy · 10 min · practice plan
If the user is logged in AND verified, show "welcome".
Passes when: #out should show welcome.
5.Guard against a missing valueexercise · medium · 10 min · practice plan
Only show the name when it is present; here it is "Sam".
Passes when: #out should show Sam.
6.Fix the assignment in the conditionbug fix · easy · 8 min
A single = makes this condition always run.
Passes when: #out should show no.
7.Fix the inverted conditionbug fix · easy · 8 min · practice plan
The branches sit on the wrong side of the test, so a 20-year-old is labelled a minor.
Passes when: #out should show adult.
8.Fix the loose equalitybug fix · easy · 8 min · practice plan
Loose equality lets a string match a number.
Passes when: #out should show different.
9.Fix the misplaced semicolonbug fix · easy · 8 min · practice plan
A semicolon right after if() ends the statement early.
Passes when: #out should stay hidden.
10.Fix the wrong logical operatorbug fix · easy · 8 min · practice plan
OR is used where both conditions must hold.
Passes when: #out should show denied.
Between them these tasks cover if statements and Boolean conditions.
Looking for a different chapter? See every practice set, or switch to the interview theory questions.