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

Answer key

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

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