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

Answer key

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

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