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

Answer key

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

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