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

Answer key

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

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