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

Answer key

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

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