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

Answer key

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

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