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

Answer key

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

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