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

Answer key

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

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