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

Answer key

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

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