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

Answer key

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

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