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

Answer key

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

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