Created
August 1, 2022 09:58
-
-
Save MVAodhan/0fa6153b218395d85bdac3ade95638df to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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