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

Answer key

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

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