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

Answer key

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

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