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

Answer key

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

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