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

Answer key

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

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