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

Answer key

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

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