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

Answer key

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

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