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

Answer key

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

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