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

Answer key

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

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