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

Answer key

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

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