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

Answer key

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

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