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

Answer key

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

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