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

Answer key

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

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