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

Answer key

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

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