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

Answer key

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

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