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

Answer key

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

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