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

Answer key

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

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