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

Answer key

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

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