Exercises
This chapter introduces no new concepts. It’s a set of exercises that pull together everything from this module: if/else if/else, switch, for, while, do...while, break, continue, and nested loops. Write and run each one in a file with node, and check your output against what’s shown.
-
Declare
const temperature = 28;. Write anif/elsethat logs"Warm"iftemperature >= 25, otherwise"Cool". Expected output:Warm -
Declare
const score = 55;. Write anif/else if/elsechain that logs"Pass"ifscore >= 60, and"Fail"otherwise. Expected output:Fail -
Declare
const hour = 9;. Write anif/else if/elsechain that logs"Morning"ifhour < 12,"Afternoon"ifhour < 18, and"Evening"otherwise. Expected output:Morning -
Declare
const day = 3;. Write aswitchondaywith cases1through5logging"Weekday", and adefaultlogging"Weekend". Rememberbreakin every case. Expected output:Weekday -
Declare
const day = 6;. Reuse the sameswitchstructure from exercise 4. Expected output:Weekend -
Write a
forloop that logs the numbers1through10, each on its own line. Expected output:1through10, one number per line. -
Write a
forloop that logs only the even numbers from2to20(usei % 2 === 0to check). Expected output:2,4,6,8,10,12,14,16,18,20, one per line. -
Declare
let total = 0;andlet n = 1;. Write awhileloop that addsntototaland incrementsnby1each time, stopping oncen > 5. Logtotalafter the loop. Expected output:15(this is1 + 2 + 3 + 4 + 5). -
Declare
let balance = 500;. Write ado...whileloop that logs"Withdrawal"and subtracts100frombalance, continuingwhile (balance > 0). Expected output:Withdrawalprinted 5 times. -
Declare
let count = 0;. Write ado...whileloop with the conditionwhile (count > 0)that logs"Running"and decrementscount. Expected output:Runningprinted exactly once, proving the body runs at least once even though the condition starts outfalse. -
Write a
forloop from1to30that usesbreakto stop as soon as it reaches a number greater than7. Expected output:1,2,3,4,5,6,7, one per line, then nothing more. -
Write a
forloop from1to10that usescontinueto skip the number5only, logging every other number. Expected output:1,2,3,4,6,7,8,9,10, one per line. -
Write a nested loop that prints a 5 by 5 multiplication table. The outer loop should run
rowfrom1to5, and for eachrow, the inner loop should build one line of text by multiplyingrow * colforcolfrom1to5(append each result plus a space onto a string with+, the same pattern used in this module’s loops lesson), then log that line. Expected output: 5 lines, where the first is1 2 3 4 5and the last is5 10 15 20 25. -
Declare
const grade = 85;. Write anif/else if/elsechain assigning a letter grade:"A"for90and above,"B"for80to89,"C"for70to79, and"F"below70. Store the result in a variable and log it. Expected output:B
If any of your answers don’t match the expected output shown, re-read the relevant section of this module before moving on. Every exercise here is meant to be checkable just by running it.
Next module: Functions, where you’ll learn to package code like this into reusable, named blocks instead of writing it out every time.