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

Answer key

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

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