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

Answer key

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

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