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

Answer key

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

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