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

Answer key

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

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