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

Answer key

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

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