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

Answer key

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

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