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

Answer key

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

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