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

Answer key

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

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