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

Answer key

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

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