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

Answer key

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

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