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

Answer key

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

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