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

Answer key

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

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