Last active
May 22, 2016 18:02
-
-
Save Krasnyanskiy/869d75ab1513756d5760c5bd880fc886 to your computer and use it in GitHub Desktop.
-js: solutions for chapter #3
This file contains hidden or 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 min(x, y) { | |
return x >= y ? y : x; | |
} | |
function isEven(n) { | |
if (n < 0) n = Math.abs(n); // alternative: n = n * -1 | |
if (n == 0) return true; | |
if (n == 1) return false; | |
return isEven(n - 2); | |
} | |
function countBs(s) { | |
var bs = 0; | |
for (var i = 0; i < s.length; i++) { | |
var ch = s.charAt(i); | |
if (ch == 'B') bs += 1; | |
} | |
return bs; | |
} | |
function _countChar(s, ch) { | |
return s.split('').filter(function (c) { | |
return c === ch; | |
}).map(function (c) { | |
return 1; | |
}).reduce(function (p, n) { | |
return p + n; | |
}, 0) | |
} | |
function countChar(s, ch) { | |
var charAmount = 0; | |
for (var i = 0; i < s.length; i++) { | |
var c = s.charAt(i); | |
if (c == ch) charAmount += 1; | |
} | |
return charAmount; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment