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

Answer key

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

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