MINIMUM
function min (a, b){
if (a < b) {
return a;
}
else {
return b;
}
}
min(7 ,1)
RECURSION
// Define a recursive function isEven
function isEven(n) {
// what if n is negative. I did not really get this part. How does this solve the problem??
if (n < 0) {
n = -n
}
// Zero is even. (Return boolean)
if (n === 0) {
return true;
}
// One is odd. (Return boolean)
if (n === 1) {
return false;
}
// If n is not (0 || 1), subtract 2 from n until it is equal to 0 or 1
return isEven(n - 2);
};
console.log(isEven(6))
// true
console.log(isEven(5))
// false
// if n == negative there is no way of subtracting to reach 0 || 1
// BEAN COUNTING
// function has a (string) as its only argument
function countBs(input) {
// create a result, set to 0 (will be returned at the end)
let result = 0;
//loop over the string
for (let i = 0; i < input.length; i++) {
//if current character is a "B"
if (input[i] === "B") {
// increment our result by 1
result++;
}
}
// return result
return result;
};
//return result
return result;
}
function countChar(string, character) {
let result = 0;
//loop over the string
for (let i = 0; i < string.length; i++) {
//if current character is a "B"
if (string[i] === character) {
// increment our result by 1
result++;
}
}
// return result
return result;
};