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

Answer key

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

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