# Chapter 3 Exercises

#102

Minimum:

``````function min(a, b){
if (a <= b){
return a;
}
else {
return b;
}
}
``````

Recursion:

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

Bean Counting:

``````function countBs(str){
let c = 0;
for(let i = 0; i < str.length; i++){
if (str[i] == "b" || str[i] == "B"){
c++;
}
}
return c;
}

function countChar(str, char){
let c = 0;
for(let i = 0; i < str.length; i++){
if (str[i] == char.toLowerCase() ||
str[i] == char.toUpperCase()){
c++;
}
}
return c;
}
``````

#103

Function MINIMUM:

``````document.write("<br><br>"+ "functions max/min" + "<br><br>");

function min(a,b) {
if (a > b)
{ return b; }
else
{return a;}
}

document.write( min(5,8) )
``````

FUNCTION IsEven

``````function IsEven(a) {
if (a==0)
{return true;}
else {
if (a==1)
{return false; }
else
{return IsEven(a-2);}
}
}

document.write( IsEven(50) )
``````

IsEven(-10) returns an error because it keeps running since it’s allready lower than 0. We can fix this by multiplying a by -1 (if a is lower than 0) so we get a positive number again.

FUNCTION countchar

``````function countBs(a) {
let countb = 0;
for (i=0; i < a.length ;i++) {
if (a[i]=='B')
{countb++;}
}
return countb;
}

document.write('Nr of B: ' + countBs('qkjfdmsBdsfqdqfBdd') + "<br><br>" )

function countChar(a,b) {
let count = 0;
for (i=0; i < a.length ;i++) {
if (a[i]== b)
{count++;}
}
return count;
}

document.write('Nr of char : ' + countChar('ThisisATest','i') + "<br><br>" )``````

#104

1. Min

``````    function min(a, b) {
if (a < b)
return a;
else
return b;
}
``````

2. Recursion

``````    function isEven(val) {
let result = "";
val = Math.abs(val);
if (val == 0)
result = "Even";
else if (val == 1)
result = "Odd";
else
result = isEven(val - 2);
return result;
}
``````

3. Count char

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

function countChar(string, char) {
let result = 0;
for (let i = 0; i < string.length; i++) {
if (string[i] == char)
result += 1;
}
return result;
}``````

#105
1. Minimum function

function min(a, b){
return Math.min(a, b);
}
var minimum = min(5, 9);

1. recursion function

var even = true;
var odd = false;

if (c == 0)
return even;
else if (c == 1)
return odd;
else if (c < 0)
return isEven(- c);
else return isEven(c - 2);
}

var result = isEven(50);
var result = isEven(75);
var result = isEven(-1);

1. Bean counting

function countChar(string, char){
let counter = 0;
for (let n = 0; n < string.length; n++){
if (string[n] == char){
counter += 1;
}
}
return counter;
}
function countBs(string){
return countChar(string, “B”);
}
console.log(countBs(“BaamBoom”));
console.log(countChar(“exercise”, “e”));

#106

Chapter 3 #1
function findMin(x, y) {
return Math.min(x, y);
}
document.write(findMin(4,5));

#107

my first exercise i did without looking here first , progressing slowly he he
3.1 Minimum
function mMin(x,y)
{if (x>y) return y;
else return x;
}
console.log (mMin(4,6));

3.2
function isEven(n) {
if (n == 0) return true;
else if (n == 1) return false;

else return isEven(n - 2);
}
console.log(isEven(54));

3.3

#108
``````    //Write a function min that takes two arguments and returns their minimum.
function minimum(x,y){
if(x<y){
return x;
}else if(x>y){
return y;
}else{
return undefined;
}
}

function isEven(number)
{
if(number==0){
return true;
console.log(number);
}else if(number==1){
console.log(number);
return false;
}else if(number<0){
return undefined;
}else{
console.log(number);
return isEven(number-2);

}
}

function countBs(inputString)
{
var numberOfBs=0;
for(var counter=0; counter<inputString.length;  counter++)
{
if (inputString[counter] == "B") numberOfBs = numberOfBs + 1;
}
return numberOfBs;
}

function countChar(inputString, CharacterToCount)
{
var numberOfBs=0;
for(var counter=0; counter<inputString.length;  counter++)
{
if (inputString[counter] == CharacterToCount) numberOfBs = numberOfBs + 1;
}
return numberOfBs;
}

#109

Minimum:
function min(a, b){
if (a < b) return a;
else return b;
}
Console.log(min(5, 6));

Recursion:
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(n));

Bean Counting:
function countChar(string, ch) {
let counted = 0;
for (let i = 0; i<string.lenght; i++) {
if (string [i] == ch) {
counted +=1;
}
}
return counted;
}
function countBs(string) {
return countChar(string, “B”);
}
console.log(countBs(“BABB”));
console.log(countChar(“kakarieku”, “k”));

#110

# 1. Minimum

function min(a,b){
if( !((typeof a == ‘number’) && (typeof b == ‘number’)) ) {return ‘Input numbers’;}

if(a<b){
return a;
}
else{
return b;
}
}

# 2. isEven()

function isEven(a){
//check if input is a number
if(!(typeof a ==‘number’){
return ‘Not a Number’;
}

if (a==0) return true;
else if (a==1) return false;
else return isEven(a-2);
}

# 3. Bean Counting

function countChar(_string,_char=‘B’) {
let count = 0;
// check if _string is string and that _char is a letter
if(typeof _string == ‘string’ && typeof _char == ‘string’ && _char.length == 1){
for(pos=0;pos<_string.length;pos++)
if(_string[pos]==_char){
count++;}
}
return count;
}

#111

1st Exercise;

``````function min(a,b) {

if ( a < b) return a;
else return b;

}
``````

2nd Exercise;

``````let isEven= n => {
//zero is even
if (n === 0) return true;
//one is even
else if (n === 1) return false;
// Negative numbers and non whole numbers returns as null so it has no value but null
else if ( n < 0 ) return null;
// for N that as a positive number evenness is N -2
else return isEven(n-2);
}
``````

3rd question

I couldn’t understand the question but from what I understand trough comments is something I did do it too.

#112

1-
function findmin(a,b){
let numb1= a;
let numb2= b;
if (numb1 < numb2){
return numb1;
}else{
return numb2;
}
}
test1 = findmin (1,4);
console.log(test1);
test2 = findmin (44,41);
console.log(test2);
test3 = findmin (4774,741);
console.log(test3);

2-
function isEven(a){
if(a < 0){
a = a*(-1)
}
if(a===0){
return true;
}
else if(a===1){
return false;
}
else{
return isEven(a-2);
}
}
test1 = isEven(50);
console.log(test1);
test2 = isEven(75);
console.log(test2);
test3 = isEven(-1);
console.log(test3);
test4 = isEven(-12);
console.log(test4);

3- function countBs(a){
counter = a.length;
var bcount = 0;
for(let i=0;i<counter;i++){
if(a[i]==‘B’){
bcount++
}
}
console.log(bcount);
}
countBs(‘balaalalaBsbsBBBss’);
function countChar(a,b){
counter = a.length;
var lettercount = 0;
for(let i=0;i<counter;i++){
if(a[i]==b){
lettercount++
}
}
console.log(lettercount);
}
countChar(‘ssaaassssBsbsBBBss’,‘a’);

#113

MinNumber:
MinNumber(3,2);
function MinNumber(num1,num2) {
if (num1<num2){console.log(num1);}
else console.log(num2);};

Recursion

``````N=75;
isEven(N);

function isEven(num) {
if (num==0){
console.log("Even");
return;}
else if (num==1) {
console.log("Odd");
return;}
else isEven(num=num-2);
}
``````

-1 cause stack overflow-- possible fix would be to check for numbers <0 before calling function.

Bean Counting

Part 1:
stringToCheck=“BubBles”;
console.log(checkString(stringToCheck));

``````  function checkString(word) {
let totalNumOfLetter=0;
for (var count=0;count<word.length;count++)
if (word[count]=="B") {totalNumOfLetter++;}
};
``````

Part 2:
stringToCheck=“BubBles”;
letterToCheck=“u”;

``````  console.log(checkString(stringToCheck,letterToCheck));
function checkString(word,letter) {
let totalNumOfLetter=0;
for (var count=0;count<word.length;count++)
if (word[count]==letterToCheck) {totalNumOfLetter++;}
};``````

#114

`<h2>MINIMUM</h2>`
`<script>`
`var a = 5;`
`var b = 5;`

``````   function getMin (m, n) {
return (m <= n ? m : n);
}

console.log(getMin(a, b));
</script>

<h2>RECURSION</h2>
<script>
var m = -6;

function isEven(a) {
if(a===0) {
console.log(`\${m} is Even`);
return true;
} else if (a===1) {
console.log(`\${m} is Odd`);
return false;
} else if (a<0) {
isEven(a+2);
} else {
isEven(a-2);
}
}

isEven(m);
</script>

<h2>BEAN COUNTING</h2>
<script>
var inputStr="bbbdefg";
var char="b";

function countChar (str, char) {
let counter = 0;
for( var i=0; i<String(str).length; i++) {
if(String(str)[i]===char) {
counter ++;
}
}
console.log(`\"\${String(str)}\" contains \${counter} \"\${char}\"s`);
}

countChar(inputStr, char);
</script>
``````

`

#115

## Minimum

``````   const minNum = function(a,b) {
if (a < b) {
document.write("The lowest number is" + " " + a);
} else document.write ("The lowest number is" + " " + b);
};

minNum(5,8);
``````

## Recursion

I got a little fancy with it, where after checking the required numbers, it prompts you to enter your own number to test.

``````        let isEven = function(n) {
if (n == 0) {
return ("even");
}else if (n == 1) {
return ("odd");
}else if (n < 0) {
return isEven(-n);
}else {
return isEven(n-2);
}
}
document.write("50 is an" + " " + isEven(50) + " " + "number" + "<br>");
document.write("75 is an" + " " + isEven(75) + " " + "number" + "<br>");
document.write("-1 is an" + " " + isEven(-1) + " " + "number" + "<br><br>");

var yourNumber = prompt("Check to see if your number is even or odd");

document.write(yourNumber + " " + "is an" + " " + isEven(yourNumber) + " " + "number");
``````

## Bean Counting

``````var str = "How much wood could a wood chuck chuck, if a wood chuck could chuck wood";
var n = str.length;

const countDs = function(str) {
var count = 0;

for (i = 0; i < n; i++) {
var char = str[i];
if (char == "d") {count++;}
}
return(count);
};

document.write("There are" + " " + countDs(str)+ " ")
document.write("D's and" + " " + n + " " + " letters in the phrase: <em>How much wood could a wood chuck chuck, if a wood chuck could chuck wood</em>");``````

#116

Minimum

function mathmin (x, y){
if (x<y) {console.log(x)
}
else {
console.log(y)
}
}

Recursion

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

Bean counting

function countChair(string, ch){
var count = 0;
for (x=0; x<string.length; x++) {
if (string[x] == ch) {
count ++;
}
}
return count;
}

function countBS(string){
return countChair(string, “B”)
}

#117

MINIMUM
function min(x,y) {
if (x<y){
console.log(x);
}else if (y<x){
console.log(y);
}else{
console.log(“x and y are the same”);
}
};

min(3,3)

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

BEAN COUNTING
function countChar (string, letter) {
let totalBs = 0;
for(let counter = 0; counter < string.length; counter ++){
if(string[counter] == letter) {
totalBs = totalBs + 1;
};
};
};

console.log(countChar(“Test This”, “T”));

#118

Calculate the minimum from two expressions:
I returned one value by multiplying two values.
I returned another value by useing the power fuction.
I then calulated the smallest of the two values.
For the value inputs I used prompts.

Result:
Multiply sum = 1476
Power sum = 5764801
The minimum is 1476

var m1 = Number(prompt(“Give first multiply number”)); // 123
var m2 = Number(prompt(“Give second multiply number”)); // 12
var p1 = Number(prompt(“Give base number”)); // 7
var p2 = Number(prompt(“Give power number”)); // 8

function multiply(a,b) {
var c = a*b;
return c;
}

function power(c,d) {
var e = Math.pow(c,d);
return e;
}

var x = multiply(m1,m2);
var y = power(p1,p2);

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

document.write("Multiply sum = "+ multiply(m1,m2) + “
”);
document.write("Power sum = " + power(p1,p2) + “
”);
document.write("The minimum is " + min(x,y));

#119

// ans 2

//ans 9 and -110

#120

Recursion

#121

Bean Counting
function countChar(a,char){
let count = 0;
for(i=0;i<a.length;i++){
a[i]==“B”?count++:{}
}
return count;
}

function countBs(a){
return countChar(a, “B”);
}

console.log(countBs(“Baby Bunnies are bouncing by the Boat Loads!”));
//ans 3