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

Answer key

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

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