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

Answer key

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

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