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

Answer key

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

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