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

Answer key

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

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