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

Answer key

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

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