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

Answer key

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

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