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

Answer key

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

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