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

Answer key

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

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