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

Answer key

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

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