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

Answer key

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

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