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

Answer key

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

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