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

Answer key

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

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