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

Answer key

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

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