// Minimum Number of Two Exercise 4.
// ------------------------------
function min(a, b) {
if (a < b) return a;
else return b;
}
document.write("
");
document.write(The smaller of the two numbers is ${min(0, 10)}
);
// --> The smaller of the two numbers 0
document.write("
");
document.write(The smaller of the two numbers is ${min(0, -10)}
);
// --> The smaller of the two numbers -10
document.write("
");
// Recursion Exercise 5.
// ------------------
function isEven(N) {
if (N < 0) N = N * -1;
if (N === 0) {
return true;
} else if (N === 1) {
return false;
} else {
return isEven(N - 2);
}
};
document.write(“The rumour your number is even is " + isEven(50));
// --> The rumour your number is even is true
document.write(”
");
document.write(“The rumour your number is even is " + isEven(75));
// --> The rumour your number is even is false
document.write(”
");
document.write(“The rumour your number is even is " + isEven(-1));
// --> The rumour your number is even is
document.write(”
");
// Bean Counting Exercise - 6.
// ----------------------
function countChar(string, ch) {
let counted = 0;
for (let i = 0; i < string.length; i++) {
if (string[i] == ch) {
counted += 1;
}
}
return counted;
}
function countBs(string) {
return countChar(string, “B”);
}
document.write(countBs(“BigboldBitcoin”));
// → 2
document.write("
");
document.write(countChar(“kentuckychicken”, “k”));
// → 3