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

Answer key

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

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