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

Answer key

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

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