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

Answer key

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

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