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

Answer key

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

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