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

Answer key

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

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