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

Answer key

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

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