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

Answer key

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

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