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

Answer key

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

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