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

Answer key

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

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