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

Answer key

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

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