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

Answer key

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

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