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

Answer key

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

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