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

Answer key

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

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