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

Answer key

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

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