MINIMUM:
function min(a, b) {
// if a is less than b
//return a
if (a < b) return a;
// otherwise return b
else return b;
}
var answer = min( 10, 11 );
alert(answer)
RECURSION:
function isEven(n) {
// if n is negative
if (n < 0) {
n = -n
}
// if n is 1
if (n === 1) {
return false
}
// if n is 0
else if (n === 0) {
return true
}
//in all other cases, apply inEven to n minus 2
else {
return isEven(n - 2)
}
}
console.log(isEven(50));
// → true
console.log(isEven(75));
// → false
console.log(isEven(-1));
// → false
BEAN COUNTING:
function countBs(string) {
// return a call to countChar with input = “B”
return countChar(string, ‘B’);
}
function countChar(string, char) {
// crete result, set to 0
let result = 0;
// loop over the string
for (let i = 0; i < string.length; i++) {
// if current character matches input character
if (string[i] === char)
//increment out result by 1
result = result + 1;
}
//return result
return result;
}
console.log(countBs(“BBC”));
// → 2
console.log(countChar(“kakkerlak”, “k”));
// → 4
I’m having hard time fully understanding Bean Counting exercise. The part we convert “B” into another character doesn’t make sense in my mind…