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

Answer key

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

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