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

Answer key

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

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