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

Answer key

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

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