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

Answer key

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

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