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

Answer key

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

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