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

Answer key

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

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