Finding a Minimum
My code
function min(x, y){
if (x < y){
return x;
}
else return y;
}
Almost the same as the EloquentJavaScript code, but then again I felt it was a relatively simple code.
isEven Function
My Code
function isEven(number){
if (number == 0){
return true;
}
else if (number == 1){
return false;
}
else {
isEven(number - 2)
}
};
Upon checking the answer, it says I should have put a “return” function, under the line “isEven(number - 2);” can anyone tell me why? Should the program have just ran the function still?
Bean Counting
My code
function countBs(input){
let counter = 0
for (N = 0; N <= input.length - 1; N++){
if (input[N] == “B”){
counter ++;
}
}
return counter;
}
function countChar(input, letter){
counter = 0;
for (N = 0; N <= input.length - 1; N++){
if (input[N] == ${letter}
){
counter ++;
}
}
return counter;
}
Now I realised that I could have put the countBs function inside countChar. And there were a few other minor mistakes. For example, in the ‘for’ function, I could have simply put the second condition as “N < input.length” instead.