Created
March 25, 2019 19:29
-
-
Save tyler-austin/78ac80988f833a2a71aadc08b9eaaf48 to your computer and use it in GitHub Desktop.
[JavaScript] Number of Occurances of a Character in a String
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 numberOfCharInString = (c, s) => { | |
let [count, index] = [0, 0]; | |
while (true) { | |
index = s.indexOf(c, index); | |
if (index >= 0) { | |
++count; | |
++index; | |
} else break; | |
} | |
return count; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment