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

Answer key

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

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