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

Answer key

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

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