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

Answer key

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

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