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

Answer key

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

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