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

Answer key

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

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