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

Answer key

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

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