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

Answer key

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

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