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

Answer key

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

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