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

Answer key

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

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