Skip to content

Instantly share code, notes, and snippets.

@tuvo1106
Created November 20, 2018 20:14
Show Gist options
  • Save tuvo1106/a4c56619f3af528b6bc6984f93dc0b71 to your computer and use it in GitHub Desktop.
Save tuvo1106/a4c56619f3af528b6bc6984f93dc0b71 to your computer and use it in GitHub Desktop.
const addWithLoops = x => {
// initializes variable sum to 0
let sum = 0
// loop through all numbers 0 to 100
for (let i = 0; i <= x; i++) {
// checks each number if is divisible by 2
if (i % 2 === 0) {
// adds number to variable sum if it is
sum += i
}
}
// return grand total
return sum
}
console.log(addWithLoops(100)) // 2550
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment