// Minimum of two numbers
function getmin(a, b) {
if (a < b) {
return a
}
else if (b < a) {
return b
}
else {
return ‘tied’
}
}
getmin (1,1); //tied
getmin (1,2); //1
getmin (2,1); //1
getmin (2,2); //tied
// Using recursion for IsEven
function isEven(numParameter) {
if (numParameter == 0) {
return true;
}
else if (numParameter == 1) {
return false;
}
else {
return isEven(numParameter - 2);
}
}
console.log(isEven(50)); // true
console.log(isEven(75)); // false
console.log(isEven(-50)); // Uncaught RangeError: Maximum call stack size exceeded
console.log(isEven(-75)); // Uncaught RangeError: Maximum call stack size exceeded
function isEven(numParameter) {
if (numParameter == 0) return true;
else if (numParameter == 1) return false;
else if (numParameter == 1) return false;
else return isEven(numParameter - 2);
}
console.log(isEven(50)); // true
console.log(isEven(75)); // false
console.log(isEven(-50)); // Uncaught RangeError: Maximum call stack size exceeded
console.log(isEven(-75)); // Uncaught RangeError: Maximum call stack size exceeded
function isEven(numParameter) {
if (numParameter == 0) return true;
else if (numParameter == 1) return false;
else if (numParameter < 0) return isEven(-numParameter);
else return isEven(numParameter - 2);
}
console.log(isEven(50)); // true
console.log(isEven(75)); // false
console.log(isEven(-50)); // true
console.log(isEven(-75)); // false
// countBs
function countBs(strInput) {
let intCount = 0;
for (let x = 0; x <= strInput.length-1; x++) {
if (strInput[x] == “b”) intCount++;
}
return intCount;
}
countBs(‘abcdef’); //1
countBs(‘abababa’); //3
countBs(‘bababab’); //4
// countChar
function countChar(strInput, chrInput) {
let intCount = 0;
for (let x = 0; x <= strInput.length-1; x++) {
if (strInput[x] == chrInput) intCount++;
}
return intCount;
}
countChar(‘abcdef’, ‘a’); //1
countChar(‘abcdef’, ‘b’); //1
countChar(‘aaabbbb’, ‘a’); //3
countChar(‘aaabbbb’, ‘b’); //4
countChar(‘bbbaaaa’, ‘a’); //4
countChar(‘bbbaaaa’, ‘b’); //3