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

Answer key

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

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