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

Answer key

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

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