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

Answer key

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

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