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

Answer key

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

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