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

Answer key

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

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