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

Answer key

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

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