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

Answer key

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

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