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

Answer key

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

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