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

Answer key

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

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