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

Answer key

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

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