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

Answer key

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

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