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

Answer key

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

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