Skip to content

Instantly share code, notes, and snippets.

@vadimkorr
Created January 28, 2018 12:23
Show Gist options
  • Save vadimkorr/d7c2c404f92b6b6cba085a8302abd521 to your computer and use it in GitHub Desktop.
Save vadimkorr/d7c2c404f92b6b6cba085a8302abd521 to your computer and use it in GitHub Desktop.
Amount of even numbers in range
// Find amount of even numbers X in range: A <= X <= B
let find = (a, b) => {
return Math.floor((b - a) / 2) + (a%2==0 || b%2==0 ? 1 : 0)
}
console.log(find(1, 5)) // 2
console.log(find(1, 4)) // 2
console.log(find(2, 5)) // 2
console.log(find(2, 4)) // 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment