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

Answer key

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

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