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

Answer key

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

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