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

Answer key

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

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