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

Answer key

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

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