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

Answer key

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

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