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

Answer key

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

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