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

Answer key

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

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