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

Answer key

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

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