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

Answer key

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

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