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

Answer key

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

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