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

Answer key

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

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