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

Answer key

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

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