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

Answer key

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

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