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

Answer key

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

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