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

Answer key

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

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