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

Answer key

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

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