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

Answer key

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

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