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

Answer key

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

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