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

Answer key

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

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