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

Answer key

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

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