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

Answer key

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

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