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

Answer key

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

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