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

Answer key

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

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