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

Answer key

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

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