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

Answer key

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

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