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

Answer key

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

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