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

Answer key

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

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