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

Answer key

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

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