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

Answer key

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

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