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

Answer key

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

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