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

Answer key

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

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