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

Answer key

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

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