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

Answer key

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

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