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

Answer key

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

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