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

Answer key

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

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