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

Answer key

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

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