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

Answer key

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

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