CC-03: For Loops

with Carmen Salas • 2024/10/07

slides

For Loops

What is an expression? An expression is any valid unit of code that resolves to a value.

For Loop Structure

for ([initial expression], [condition expression], [increment expression]) {
    [statement/s]
}

A for loop repeats until the condition expression evaluates to false.

While Loop Structure

while ([condition expression]) {
    [statement/s]
}

while and for loops are the same, and you can always convert one to the other!

Code Challenge

  1. Write a function named countToTen that console.logs all the integers from 1 to 10.

  1. Write a function named countUpFromOne that takes in an integer argument, and console.logs all the integers from 1 up to the given integer.

  1. Write a function named countDownFromN that takes in an integer argument, and console.logs all the integers from the given integer to 1.

  1. Write a function named countEveryEven that takes in an integer argument, and console.logs all the even integers from 1 up to the given integer, including the given integer.

  1. Write a function named countEveryOdd that takes in an integer argument, and console.logs all the odd integers from 1 up to the given integer, including the given integer.

BONUS

  1. Write a fucntion named countEvens that takes in an array of integers and returns the number of integers in the array that are even numbers.

Last updated