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

Answer key

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

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