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));


#208

ALL WORKING

1. Minimum

function smallestNum(...numbersArray) {
    lowestNumber = true;
    numbersArray.forEach(number => {
        if (lowestNumber == true) {
            lowestNumber = number;
        }
        if (lowestNumber > number) {
            lowestNumber = number
        }
    });
    console.log("Currently the lowest number is " + lowestNumber); // for testing purposes
    return lowestNumber // for production purposes
}

2. Even or Odd

function determineIfEven(num1) {
    switch (!isNaN(num1)) {
        case (num1 == 0):
            console.log(true);
            break;

        case (num1 == 1):
            console.log(false);
            break;

        case (num1 > 1 || num1 < 0):
            if (num1 > 1) {
                while (num1 > 1) {
                    num1 -= 2;
                }
            } else {
                while (num1 < 0) {
                    num1 += 2;
                }            
            }
            determineIfEven(num1); 
        
        default:
            break;
    }
}

3. Bean Counter

function countBs(stringInput) {

    stringInput = String(stringInput);
    let letterArray = Array.from(stringInput), count = 0;

    letterArray.forEach(letter => {
        if (letter == "B" && letter == letter.toUpperCase()) {
            count++;
        };
    });
    return count;
}

function countChar(stringInput, charSelect) {

    stringInput = String(stringInput);
    let letterArray = Array.from(stringInput), count = 0;

    letterArray.forEach(letter => {
        if (letter == charSelect) {
            count++;
        };
    });
    return count;
}

#209

Bean Counter
this one worked for me:

function countBs(string){

let resultB=0;
let resultA=0;
for (let count=0; count<string.length; count++){
string[count]==“b” ? resultA ++ : resultA=resultA;
string[count]==“B” ? resultB ++ : resultB=resultB;
}
return [resultA, resultB];
}
let answer=countBs(“BeansBeansBbbBeans”);
alert(answer);


#210

Minimum

function min(a, b) {
if(a < b)
return a;
return b;
}
console.log(min(11, -10));

Recursion

function isEven(a) {
if (a < 0)
a *= -1;
if(a === 0) {
console.log(‘even’);
} else if(a === 1){
console.log(‘odd’);
} else {
isEven(a-2);
}
}
isEven(-1);

Bean Counting

function countChar(searchString, letter) {
let count = 0
for(let i = 0; i < searchString.length; i++) {
if (searchString[i] == letter)
count++;
}
return count;
}
console.log(countChar(‘Assassination’, ‘a’));


#211

1_ Min

function min(x,y){
    if (x<y) {
      return x;
    }
    else {
      return y;
    }
  }

  console.log(min(11,12));

2_Recursions

 function isEven(x){
    let num = Math.abs(x);
    if (num == 0) {
      return true;
    }
    else if (num == 1){
      return false;
    }
    else return  isEven (num-2);
  }
  console.log(isEven(-2));

For this exercise I lost myself trying to look for a way to use a for loop to induce recursion. it was thoroughly unsuccessful, I had to look for the answer to move forward.

3_1_

 function countBs(string){
   let count = 0;
   for (let i = 0; i < string.length; i++){
       if (string[i] == "B") {
           count += 1;
       }
   }
   return count;
}

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

3_2_

function countChar (string, x){
   let count = 0;
   for (var i = 0; i < string.length; i++) {
     if (string[i] == x){
       count += 1;
     }
   }
   return count;
 }

console.log(countChar("foot", "o"));

3_3

 function countChar (string, x){
   let count = 0;
   for (var i = 0; i < string.length; i++) {
     if (string[i] == x){
       count += 1;
     }
   }
   return count;
 }

 function countBs(y){
   return countChar(y, "B")
}

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

I struggled ridiculously on this one again, I had to look it up for the first one. then did the second and third by myself.


#212

Minimum

function minimum(n1,n2){
  if(n1 > n2){
    return n2;
  } else {
    return n1;
  }
}

console.log(minimum(10,6));

Recursion

function isEven(n1){
  if(n1 == 0) {
    return true;
  } else if(n1 == 1) {
    return false;
  } else if(n1 < 0) {
    return isEven(-n1);
  } else {
    return isEven(n1 - 2);
  }
}

console.log(isEven(-50));

Bean Counting
I wanted to try something different from just the for loop, as that seemed to miss the point of the exercise for me considering we just were taught about Recursion. So I used a function’s ability to call on itself, but I struggled to get it done. The issues I had were declaring a global variable and passing ruturn statements from the one function to the other. I had this idea that as long as you have a return in the final function that it would automatically pass to the previous function, this took me 2 hours to figure out that a return need to be placed on the in the countBs() function on the numB() function., but I learned a lot.

var numB = 0;
function numBs(wrds, wLength, numB){
  if(wLength > 0 && wrds[wLength - 1] === "B") {
    numB = numB + 1;
    wLength = wLength - 1;
    return numBs(wrds, wLength, numB);
  } else if(wLength !== 0 && wrds[wLength -1] !== "B"){
    wLength = wLength - 1;
    return numBs(wrds, wLength, numB);
  } else if(wLength === 0){
    return numB;
  }
}

function countBs(wrds){
var wrdsLgth = wrds.length;
return numBs(wrds, wrdsLgth, numB);
}
console.log("Number of Cap B’s: "+ countBs(“BBbbbBBbbb”));

Character Location
This showed me that the previous program I made had some major issues, being the global variable would prevent me for repeating the function without resting it’s values. The solution was the pass the variable instead.

function numBs(wrds, wrdsLgth, numB, kar, numB, sLoc){
  if(wrdsLgth > 0 && wrds[wrdsLgth - 1] === "B") {
    if (wrds[wrdsLgth -1] === kar){
      sLoc = wrdsLgth +" "+ sLoc;
    }
    numB = numB + 1;
    wrdsLgth = wrdsLgth - 1;
    return numBs(wrds, wrdsLgth, numB, kar, numB, sLoc);
  } else if(wrdsLgth !== 0 && wrds[wrdsLgth -1] !== "B"){
    if (wrds[wrdsLgth -1] === kar){
      sLoc = wrdsLgth +" "+ sLoc;
    }
    wrdsLgth = wrdsLgth - 1;
    return numBs(wrds, wrdsLgth, numB, kar, numB, sLoc);
  } else if(wrdsLgth === 0){
    return numB+" Character locations for "+kar+": "+sLoc;
  }
}

function countBs(wrds, kar){
var numB = 0;
var sLoc = “”;
var wrdsLgth = wrds.length;
return numBs(wrds, wrdsLgth, numB, kar, numB, sLoc);
}

console.log("Number of Cap B's: "+ countBs("BBbbbBBbbb", "b"));
console.log("Number of Cap B's: "+ countBs("BBbbbBBbbb", "B"));