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

Answer key

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

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