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

Answer key

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

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