Skip to content

Instantly share code, notes, and snippets.

@kylelong
Last active August 6, 2022 03:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kylelong/fa41e9b68ee452926f9d64d09ea7457a to your computer and use it in GitHub Desktop.
Save kylelong/fa41e9b68ee452926f9d64d09ea7457a to your computer and use it in GitHub Desktop.
const numberOfOnes = (num) => {
let sum = 0;
for(let i = 0; i <= num; i++){
sum += i.toString().split("").filter(number => number === '1').length;
}
return sum;
}
console.log(numberOfOnes(14)); //7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment