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

Answer key

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

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