Chapter 3 Exercises


#205

Minium:
let Output = function Inputs(a,b){
mini = Math.min(a,b);
return mini
}
console.log(Output(0,-5))

Recursion:
/* Output will be called to get the answer*/
let Output = function isEven(N) {
/* N represent the number users will input*/
N = Math.abs(N)
return (N % 2) == 0;
/* When the user enters a number for N /
/
the program will check if there is a reminder*/
/* no remender means N is even*/
function isOdd(n) {
return (N % 2) == 1;}
/* If there is a reminder than the N is odd*/
function reReturn(N){
return isEven(N - 2)
}
}
console.log(Output(75));

Bean Master:
function countChar(string, char) {
let count = 0;
for (let i = 0; i < string.length; i++) {
if (string[i] == char) { count++ }
}

return count;
}

function countBs (string){
return countChar(string,“B”)

}
console.log(countBs(“Barbados”))
console.log(countChar(“Canada”,“a”));


#206

Bean Counting

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, "D");

}

console.log(countBs("DAVIDOFIDO"));

// -> 3

console.log(countChar("kokkoriko", "k"));

// -> 4
RECURSION

function isEven(num){

num = Math.abs(num);

if(num === 0)

return "number is even";

else if(num === 1)

return "number is odd";

else

return isEven(num - 2);

}

console.log(isEven(50));

console.log(isEven(75));

console.log(isEven(-2));

Minimum

function min(y,x){

if(y < x) return y;

else

return x;

}

console.log(min(75,105));

//->75

console.log(min(31, 55));

//->31


#207

isEven function to determine whether a number is odd or even

function isEven(N){
if (N==0){
return “Even”;
}
else if (N==1){
return “Odd”;
} else if (N<1){
return isEven(N+2);
} else {
return isEven(N-2)
}
}
console.log (isEven (-11));