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

Answer key

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

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