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

Answer key

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

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