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

Answer key

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

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