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

Answer key

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

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