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

Answer key

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

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