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

Answer key

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

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