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

Answer key

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

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