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

Answer key

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

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