Skip to content

Instantly share code, notes, and snippets.

@MVAodhan
Created August 1, 2022 09:58
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 MVAodhan/0fa6153b218395d85bdac3ade95638df to your computer and use it in GitHub Desktop.
Save MVAodhan/0fa6153b218395d85bdac3ade95638df to your computer and use it in GitHub Desktop.
const numberOfOnes = (num) => {
let total = 0;
let arraysToEval = [];
for (let i = 1; i <= num; i++) {
let iStr = i.toString();
let iStrArray = [...iStr];
if (iStrArray.indexOf('1') != -1) {
arraysToEval = [...arraysToEval, iStrArray];
}
};
for(let arr of arraysToEval){
for (let digit of arr){
if (digit == '1'){
total += 1;
}
}
}
console.log(total)
return total
}
console.log(numberOfOnes(14))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment