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

Answer key

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

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