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

Answer key

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

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