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

Answer key

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

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