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

Answer key

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

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