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

Answer key

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

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