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

Answer key

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

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