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

Answer key

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

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