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

Answer key

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

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