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

Answer key

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

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