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

Answer key

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

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