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

Answer key

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

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