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

Answer key

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

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