← JavaScript if and for loops lesson New set →

JavaScript Worksheet: If and For Loops

HTML and CSS lesson 26: JavaScript if and for loops · Set 99 · 40 questions
TalentJR

Code tip (Check, then count): if (condition) { … } else { … }: only one of the two parts runs. for (let i = 0; i < 5; i++) runs for i = 0, 1, 2, 3, 4 — 5 times. With i <= n it includes n; with i = i + 2 it goes up in twos.

NameDateTime takenScore ___ / 40
  1. 1.🔁 What does this code print?
    let score = 23;
    if (score < 5) {
      console.log("You win");
    } else {
      console.log("Play again");
    }
    (a) Play again (b) You win
  2. 2.🔁 What is the LAST number this code prints?
    for (let i = 0; i < 5; i = i + 3) {
      console.log(i);
    }
  3. 3.🔁 What does this code print?
    let age = 25;
    if (age > 12) {
      console.log("teen");
    } else {
      console.log("child");
    }
    (a) child (b) teen
  4. 4.🔁 How many times does this code print “Welcome”?
    for (let i = 2; i <= 7; i++) {
      console.log("Welcome");
    }
  5. 5.🔁 What does this code print?
    let score = 42;
    if (score > 9) {
      console.log("You win");
    } else {
      console.log("Play again");
    }
    (a) Play again (b) You win
  6. 6.🔁 How many numbers does this code print?
    for (let i = 0; i < 5; i = i + 2) {
      console.log(i);
    }
  7. 7.🔁 What does this code print?
    let temp = 5;
    if (temp < 6) {
      console.log("Hot");
    } else {
      console.log("Cool");
    }
    (a) Hot (b) Cool
  8. 8.🔁 How many times does this code print “Hello”?
    for (let i = 1; i < 4; i++) {
      console.log("Hello");
    }
  9. 9.🔁 What does this code print?
    let temp = 38;
    if (temp > 34) {
      console.log("Hot");
    } else {
      console.log("Cool");
    }
    (a) Hot (b) Cool
  10. 10.🔁 How many numbers does this code print?
    for (let i = 0; i < 20; i = i + 2) {
      console.log(i);
    }
  11. 11.🔁 How many times does this code print “Hello”?
    for (let i = 3; i < 11; i++) {
      console.log("Hello");
    }
  12. 12.🔁 What does this code print?
    let temp = 10;
    if (temp >= 27) {
      console.log("Hot");
    } else {
      console.log("Cool");
    }
    (a) Hot (b) Cool
  13. 13.🔁 Which word runs code only when something is true? (a) log (b) for (c) if (d) let
  14. 14.🔁 What does this code print?
    let age = 28;
    if (age < 26) {
      console.log("teen");
    } else {
      console.log("child");
    }
    (a) teen (b) child
  15. 15.🔁 What is the LAST number this code prints?
    for (let i = 0; i < 16; i = i + 4) {
      console.log(i);
    }
  16. 16.🔁 How many numbers does this code print?
    for (let i = 0; i < 11; i = i + 3) {
      console.log(i);
    }
  17. 17.🔁 What number does this code print?
    let total = 0;
    for (let i = 1; i <= 6; i++) {
      total = total + i;
    }
    console.log(total);
  18. 18.🔁 Which loop repeats code a set number of times? (a) else (b) let (c) if (d) for
  19. 19.🔁 How many numbers does this code print?
    for (let i = 0; i < 14; i = i + 3) {
      console.log(i);
    }
  20. 20.🔁 How many times does this code print “Well done”?
    for (let i = 1; i <= 5; i++) {
      console.log("Well done");
    }
  21. 21.🔁 What does this code print?
    let age = 6;
    if (age > 15) {
      console.log("teen");
    } else {
      console.log("child");
    }
    (a) teen (b) child
  22. 22.🔁 What is the LAST number this code prints?
    for (let i = 0; i < 18; i = i + 2) {
      console.log(i);
    }
  23. 23.🔁 How many numbers does this code print?
    for (let i = 0; i < 17; i = i + 4) {
      console.log(i);
    }
  24. 24.🔁 What does this code print?
    let age = 7;
    if (age >= 36) {
      console.log("teen");
    } else {
      console.log("child");
    }
    (a) child (b) teen
  25. 25.🔁 What does this code print?
    let score = 8;
    if (score >= 25) {
      console.log("You win");
    } else {
      console.log("Play again");
    }
    (a) You win (b) Play again
  26. 26.🔁 How many times does this code print “Well done”?
    for (let i = 3; i < 7; i++) {
      console.log("Well done");
    }
  27. 27.🔁 How many times does this code print “Hi”?
    for (let i = 0; i < 6; i++) {
      console.log("Hi");
    }
  28. 28.🔁 What is the LAST number this code prints?
    for (let i = 0; i < 15; i = i + 2) {
      console.log(i);
    }
  29. 29.🔁 How many numbers does this code print?
    for (let i = 0; i < 14; i = i + 4) {
      console.log(i);
    }
  30. 30.🔁 What number does this code print?
    let total = 0;
    for (let i = 1; i <= 5; i++) {
      total = total + i;
    }
    console.log(total);
  31. 31.🔁 What is the LAST number this code prints?
    for (let i = 0; i < 9; i = i + 3) {
      console.log(i);
    }
  32. 32.🔁 What does this code print?
    let temp = 48;
    if (temp > 25) {
      console.log("Hot");
    } else {
      console.log("Cool");
    }
    (a) Cool (b) Hot
  33. 33.🔁 How many times does this code print “Welcome”?
    for (let i = 2; i <= 5; i++) {
      console.log("Welcome");
    }
  34. 34.🔁 How many times does this code print “Hello”?
    for (let i = 3; i < 6; i++) {
      console.log("Hello");
    }
  35. 35.🔁 What does i++ do? (a) doubles i (b) adds 1 to i (c) takes 1 from i (d) prints i
  36. 36.🔁 When does the else part run? (a) when if is false (b) always (c) never (d) when if is true
  37. 37.🔁 How many times does this code print “Hello”?
    for (let i = 1; i <= 3; i++) {
      console.log("Hello");
    }
  38. 38.🔁 What does this code print?
    let age = 42;
    if (age < 34) {
      console.log("teen");
    } else {
      console.log("child");
    }
    (a) teen (b) child
  39. 39.🔁 How many numbers does this code print?
    for (let i = 0; i < 8; i = i + 2) {
      console.log(i);
    }
  40. 40.🔁 How many times does this code print “Hello”?
    for (let i = 0; i < 2; i++) {
      console.log("Hello");
    }

Answer key

  1. Play again
  2. 3
  3. teen
  4. 6
  5. You win
  6. 3
  7. Hot
  8. 3
  9. Hot
  10. 10
  11. 8
  12. Cool
  13. if
  14. child
  15. 12
  16. 4
  17. 21
  18. for
  19. 5
  20. 5
  21. child
  22. 16
  23. 5
  24. child
  25. Play again
  26. 4
  27. 6
  28. 14
  29. 4
  30. 15
  31. 6
  32. Hot
  33. 4
  34. 3
  35. adds 1 to i
  36. when if is false
  37. 3
  38. child
  39. 4
  40. 2

Free html and css lessons, practice and worksheets at talentjr.in/html-css-for-kids