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

Answer key

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

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