This Function returns the minimum of the pair. The function uses the less than operator to return the value of the x variable or else it returns the value of the Y variable depending which is less than.
function min(x,y) {
if (x<y) return x;
else return y;
}
console.log(min(?,?));
################################################################
function isEven(n) { /////<–single input perameter
if (n == 0) return true;
else if (n == 1) return false;
else if (n < 0) return isEven(-n);
else return isEven(n - 2);
}
console.log(isEven(?));
The first statement is an IF statement and it determines what if the answer is true by checking if it is equal to zero if not the if passes the value to the else statement and that checks if it is false by compareing it to the number 1 then if not then it passes the value to the final else statement which involke it self within the statement. it takes the value and subtract the value by 2 and it then repeats all the if and else statement until it reduces to the number 1 or 0 and then and give the output. True or False. By adding this Else statement to the function the code will return the eveness of negitive numbers by turning the negatie number postive. and then passing it on to the reset of the statements. ie. else if (n < 0) return isEven(-n);
now the code looks like such :
function isEven(n) {
if (n == 0) return true;
else if (n == 1) return false;
else if (n < 0) return isEven(-n);
else return isEven(n - 2);
}
console.log(isEven(?));
a better code:::::
function isEven(N){
if(N!=0 && N!=1 && N>=2){
N-=2;
isEven(N);
}
else if (N==0){
toReturn = “This is an even number”
}
else if (N==1){
toReturn = “This is an odd number”
}
else {
isEven(-N);
}
return toReturn;
}
console.log(isEven(?));
:::::::::::::::code with negitive included::::::::
function isEven(N){
if(N!=0 && N!=1 && N>=2){
N-=2;
isEven(N);
}
else if (N==0){
toReturn = “This is an even number”
}
else if (N==1){
toReturn = “This is an odd number”
}
else if (N==-0){
toReturn = “This is a Negitive even number”
}
else if (N==-1){
toReturn = “This is a Negitive odd number”
}
else {
isEven(-N);
}
return toReturn;
}
#################################################################
This Code is to Count the letter B’s within a sentence
with use of a single peramiter function one can create a function called countBs.By using let stringLength equal aString.length and let numofBs=0; as a constent then a for loop is created to read the sentence at hand then conditions can be introduced to the code, if aString [i] is to define the sentence is equal to “B” then with use of numofBs+=1 this conditions assures the counting of the B’s. and with the addition of return numofBs; will give use the sum of capital Bs , i have also incuded the second code to count lower case b’s. the third code will count words in a sentence, using charcount function.
function countBs(aString){
let stringLength = aString.length;
let numOfBs=0;
for(i=0;i<stringLength;i++){
if(aString[i]==“B”){
numOfBs+=1;
}
}
return numOfBs;
}
console.log(countBs(“Boston Bake Beans and beer for Beerfest”));
::::::::::Code 2
function countBs(aString){
let stringLength = aString.length;
let numOfBs=0;
for(i=0;i<stringLength;i++){
if(aString[i]==“B”){
numOfBs+=1;
}
else if(aString[i]==“b”){
numOfBs+=1;
}
}
return numOfBs;
}
console.log(countBs(“Insert Sentence That have Bs here Bro.”));
::
second part of the code question::
function countChar(newString,aChar){
let stringLength = newString.length;
let count=0;
for(i=0;i<stringLength;i++){
if(newString[i]==aChar){
count+=1;
}
}
return count;
}
console.log(countChar(“Pop Stars Popping Bottles”));