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

Answer key

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

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