Created
February 23, 2015 23:26
-
-
Save kylebakerio/f00d4447d2cb6f8b49c2 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
function countChars(string, char){ | |
b = 0; | |
for (i=0;i<string.length;i++){ | |
if (string.charAt(i) === char) b++ | |
} | |
return b | |
}; | |
countChars("BBbbbcccdsdhshBBzb", "b") |
trrichard
commented
Feb 24, 2015
- Don't name your variables after primitives, this can cause problems later down the road. Try to use more descriptive names. In small samples like this it won't cause a problem. Suggested Names: s/string/stringToScan/ and s/char/charToSearchFor/. Or something like that.
- Don't use single letter variable names. Using an "i" for a for loop is the only exception. s/b/count/
- You forgot a ; after your return statement. In js I don't think similcolons are significant (ie your program won't crash if you missed them) so you should decide up front: do I want to use these? If so then use them at the end of all lines.
- I would recommend avoiding the use of inline if blocks. Always use the {}. It helps with readability and If you have to add another statement the {} is already there.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment