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

Answer key

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

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