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

Answer key

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

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