MINIMUM:
function min(x,y) {
isLess = x < y;
if (isLess) {
return x;
} else{ return y;
}
}
Once given two arguments, the variable isLess
will compare them and with the use of conditionals either value will be returned accordingly. If the 1st value is less than the 2nd the function will return the 1st value. Else if the 1st value is not less than the 2nd value the function will return the second value. If the values are equal it will return the 2nd value, this choice is irrelevant since both arguments are the same.
RECURSION
function isEven(x){
if (x === 0) { return true;
} else if (x === 1) { return false;
} else if ( x < 0 ) { return isEven(-x);
} else return isEven(x-2)
}
This function’s goal is to check if a given number is even. For this first we show the console two comparisons, one that returns true “Even Number” (x===0)
and one that returns false "Odd Number (x===1)
. The console first checks these two conditional statements and if they both return false, it checks if the value is less than 0 if the result of this comparison is true
then it will take the value and run it through the function isEven
again but this time with the minus operator before it, converting it into a positive number (negative and negative becomes postive, -(-x) = x. Once it’s a positive value then it runs again through the function, after checking the first two comparisons without success it carries on to the else
statement which will substract 2 from the number and run the result through the function over and over again until it reaches either 1 or 0, so it can return the Boleean value required.
BEAN COUNTING:
-CountBs function
function countBs(string){
var addition = 0;
for (var i = 0; i < string.length; i++){
if(string[i] === "B") { addition ++;
}
}
return addition;
}
This function’s goal is to count the amount of "B"s for a given string. First we create a variable that keeps count, the we create a loop that goes through each character of the given string and within the loop we set a conditional statement that checks wheather each character is a “B” or not if this comparison is true then the 1 is added to the counting variable addition
. Once the loop has gone through every character, it return the final sum of “B” characters that it counted.
-CountChar function
function countChar (string,char){
var addition = 0;
for(var i = 0; i < string.length; i++){
if(string[i] === char){
addition ++;}
}
return addition;
}
Much like the previous function, this one those basically the same thing but gives us more freedom since it checks any character that we provide, since a new parameter has been added to it. Now instead of checking for the character “B” as it iterates through the loop, now it compares wether the given char
argument is equal to the character that’s being checked by the loop in each iteration. If it matches then 1 is added to the counting variable addition
. Once the loop has gone through every character, it returns the final sum of char
characters that it counted.