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

Answer key

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

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