# Chapter 3 Exercises

#122

var isEven = function(num) {
num = Math.abs(num); //convert to absolute value to account for negative numbers
if (num === 0)
return true;
else if (num === 1)
return false;
else
return isEven(num - 2)
};

#123

document.write(Math.min( 2, 4, 5));

// ans 2

//ans 9 and -110

#124

Ok, here’s what I have so far for the assignments

Min
function min(a,b){
if( a > b){
return (b + " " + “Is smaller” + );

``````     }
else if (a < b){
return ( a+ " " +"Is smaller" + </br>);
}
else (a == b)
return ( "They are equal" + </br>)
}``````

#125

Here’s my is even program

``````function isEven(n) {
``````

if (n==0){

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

#126

Counting B’s

function countBs(str) {
var count = 0;
for (var i = 0; i < str.length; i++) {
if (str.charAt(i) === “B”) {
count++;

``````    }
}
return (count);
``````

I coulnt’ get it to do the lower case “b’s” Any suggestion on how to get it to count both the large and small b’s? Any help is appreciated

#127

EX 1 Minimum

``````var a=prompt("Enter 1st", "");
var b=prompt("Enter 2nd", "");
min = (a,b) => {
if (a<b) {
return (a);}
else return (b)
}
``````

EX2 Recursion

``````var a=prompt("Enter the number", "");
isEven = (a) => {
if (a<0) {
return false;
}
else if (a%1==0){
return true;
}
else {
isEven(a-2);
}
}
console.log(isEven(a));
``````

EX3 Bean counting

``````      var str=prompt("Enter the string");
var charUpp=prompt("Enter what uppercase to count");
var char2nd=prompt("Enter what 2nd char to count");
var charCount="";
countsBs = (charCount) => {
var countBeans = 0;
for (n=0;n<str.length;n++)
if (str[n]==charCount){
countBeans++;
}
return countBeans;
}

countChar = (charUpp,charCount) => {
var countUpp = 0;
var count2nd = 0;
countsBs (charUpp);
countUpp=countsBs(charUpp);
countsBs (char2nd);
count2nd=countsBs(char2nd);
return ("There are " + countUpp +" "+ charUpp+ " & "+ count2nd + " "+ char2nd + " in the "+str);
}
console.log(countChar(charUpp,charCount));``````

#128

Bean counting
function countBs(Bstring,BChar) {
let Bs = 0;
for (count = 0; count < Bstring.length; count ++) {
if(Bstring[count] == BChar) {
Bs += 1;
}
}
return Bs;
}

document.write(countBs(“BoBbejaan”,“B”)); // =2

#129

# Chapter 3

### 1. Minimum

The code asks the user to enter 2 numbers and returns the smaller number.
``````a = prompt("Enter number 1");
b = prompt("Enter number 2");

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

### 2. Recursion

The exercise was to write a recursive function that mimic's %2.
``````c = prompt("Enter  a number");
function isEven(n){
let a = n;
if(a == 0){
return 0;
}else if(a == 1){
return 1;
}else if(a < 0){
a = a * a;
return isEven(a)
}else {
a = a - 2;
return isEven(a);
}
}
document.write(isEven(c));
``````

### 3. Bean Counting

The Exercise was to write a function ,countBs, that returns the number of "B"s in a string. Than write another function countChar that counts any given character in a string. Next was to rewrite countBs to use countchar. c = prompt("Enter a string"); d = prompt("Letter to be counted");
``````function countChar(n,chartoBeCounted)
{
let counter = 0;
let NumOfBs = 0;
do{
if (n[counter] == chartoBeCounted){
NumOfBs += 1;
}

counter += 1;
}
while(n[counter] != undefined);

return NumOfBs;
}

function countBs(n){
return countChar(n,"B");
}

document.write("There are " + countChar(c,d) + " " + d + " in the string.");

document.write("<br /> There are " + countBs(c) + "B s in the string");``````

#131

MY 3 EXCERCISES BUNDELED

``````let b=false;

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

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

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

#132
``````function minimum(x, y){
if(x < y){
return x;
} else {
return y;
}
}``````
``````function isEven(n){
if(n < 0){
return isEven(n * -1);
} else if(n === 0){
return true;
} else if(n === 1){
return false;
} else {
return isEven(n-2);
}
}``````
``````function countCh(str, ch){
let count=0;
for(let i=0; i<str.length;i++){
if(str[i]==ch){
count++;
}
}
return count;
}
function countBs(str){
return countCh(str, "B");
}``````

#133
1. ### Minimum

function min(x,y){
if (x<y){return x;}
else {return y;}
}
2. ### Recursion

function isEven(n){
let num = Math.abs(n)
if (num == 0){return true;}
else if (num == 1){return false;}
else {return isEven(num-2);}
}
3. ### Bean Counting

1. #### First CountBs Function Assignment

function countBs(checkword){
var cntBs=0;
for (n=0;n<checkword.length;n++){
if (checkword[n] == 'B'){cntBs++;}
}
return cntBs;
}
2. #### Second countBs Function Assignment

function countChar(checkword,checkChar){
var cntChar=0;
for (n=0;n<checkword.length;n++){
if (checkword[n] == checkChar){cntChar++;}
}
return cntChar;
}

function countBs(checkword){
return countChar(checkword,"B");
}

#134

MINIMUM - Version 1
Calling from outside the body.

``````  function min(a,b) {
if (a<b){ answer = a; }
else { answer = b; }
document.write("The smaller number of the two is: " + answer);
}

// These 2 numbers as an example to call the function...

min(35,42);
``````

MINIMUM - Version 2
With alert prompts to request input from user, and adding a 3rd option when both values are the same.

``````var a = prompt ("Enter the FIRST number you wish to compare:", "...");
var b = prompt ("Enter the SECOND number you wish to compare:", "...");

function min(a,b) {
if (a == b) { document.write ("Both numbers are the same!"); }
else { if (a < b) {return a;}
else {return b;}
}
}
document.write("The smaller number of the two is: " + min(a,b));
``````

RECURSION
I wanted to get a little fancy with strings and also have the capability of storing the original function parameter (a) as an out-of-scope variable, so that I could later add it to the returned string.

``````var a = prompt ("Enter the number you wish to evaluate:", "...");
var original = a;
if (a < 0) { a = Math.abs(original); }

function isEven(a) {
if (original == 0) return "The number you entered (" + original + ") is a ZERO.";
if (a == 0) return "The number you entered (" + original + ") is an EVEN number.";
else if (a == 1) return "The number you entered (" + original + ") is an ODD number.";
else if (a < 0) return isEven(a);
else return isEven(a - 2);
}
document.write (isEven(a));
``````

BEAN COUNTING (Version 1: Counts upper-case "B"s)

``````let mySentence = prompt ("Enter the text you'd like to bean-count:", "...");

function countBs(mySentence) {
let countBs = 0;
for (var n = 0; n < mySentence.length; n ++ ){
if (mySentence[n] === "B") {countBs ++;}
}
return countBs;
}
document.write(
"The string you entered (" + mySentence + ") contains: "
+ countBs(mySentence) + " Beans");
``````

BEAN COUNTING (Version 2: Counts any given string)

``````let mySentence = prompt ("1. Enter the text you'd like to bean-count:", "...");
let myBean = prompt ("2. Enter your Bean", "...")

function countChar(mySentence, myBean) {
let countChar = 0;
for (var n = 0; n < mySentence.length; n ++ ){
if (mySentence[n] == myBean) {countChar ++;}
}
return countChar;
}
document.write(
"The string you entered <br /> (" + mySentence + ") <br /> Contains: "
+ countChar(mySentence, myBean) + " Beans (" + myBean + ")");``````

#135

1. MINI

``````function mini(a, b) {
console.log(Math.min(a, b));
}
mini(0, 3.4);
``````

2. Recursion

``````function isEven(a) {
if (a >= 0 && a%2 == 0) {
console.log(a + " is even");
} else {
console.log(a + " is odd");
}
}

isEven(-1)
``````

3. Bean

`````` function countChar(string, ch) {
let counted = 0;

for (i = 0; i < string.length; i++) {
if (string[i] == ch) {
counted++;
}
}
console.log(counted);
}

countChar("VVVbCB", "B")``````

#136

Exercise 3:

Bean Counter

# Bean Counter

``````<script>

function CountChar(str, char) {
let counter = 0;
for (i = 0; i <= str.length - 1; i++) {
if (str[i] == char) {
counter++;
}
}
return counter;
}

var Name1 = "Curtis Manwaring";
var Char1 = "z";
var Count1 = CountChar(Name1, Char1);

if (Count1 == 1) {
document.write("<p>There is " + Count1 + " " + Char1 + " in " + Name1 + ".</p>");
} else {
document.write("<p>There are " + Count1 + " " + Char1 + "'s in " + Name1 + ".</p> ");
}

</script>
``````

Exercise 2:

Recursion function

# Recursion Function

``````<script>
var A = -129;

function isEven(n) {
let R = false;

if (n < 0) {
n *= -1;
}

while (n >= 2) {
n -= 2; // could have written a function for the while here called
// Reduce() so that this is truly recursive but this is simpler
}

if (n == 0) {
R = true;
} /* else {
R = false;
} */ // since we isolated to zero or one with the initialization of
// R = false we need only test if n is zero or not
return R;
}

let number = isEven(A);

if (number == true) {
document.write("<p>The number " + A + " is even.</p>");
} else {
document.write("<p>The number " + A + " is odd.</p>");
}
</script>
``````

Exercise 1:

Minimum function

# Minimum Function

``````<script>
var A = 22;
var B = 13;

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

var M = min(A, B);

document.write("<p>The minimum of " + A + " and " + B + " is " + M + "</p>");
</script>
``````

#137

#138

function min(a, b) {
if (a < b) {return a}
else {return b}
}
I wrote it with curly brackets and it works, but why they don’t use curly brackets in the answers?

#139

I wrote that function for the second exercise, it works perfectly, but there is very diffrent function in the answers. Thats my function:

function isEven(x){
if (x%2==0) {
console.log(“is Even”)
}else {console.log(“is not Even”)}

}

#140

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, “B”);
}

console.log(countBs(“BBC”));
// → 2
console.log(countChar(“kakkerlak”, “k”));
// → 4

#141

MINIMUM

The simplest way to check for a minimum value is to use the Math.min function as follows -
function mincheck(a,b){
return Math.min(a,b)
}

console.log(mincheck(20,500));

The same can be acheived by using a few if statements to test for the larger value as follows -

mincheck=(a,b) => {
if (a>b) {
return b;}
else{
return a;
}
}

console.log(mincheck(40,500));

RECURSION

Using recursion we can subtract 2 from the given number untill the output is either 1 or 0
We can also add a test to see if the origional passed value is a negative number and if so change it to positive before carrying out the further statements

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

console.log(isEven(-50));

BEAN COUNTER
To count a matching letter in a string can be done by using a for loop through the string and checking for the “letter”, if it is found it increases a counter.

Once the string test is complete the counter value is returned and outputted.

function countBs(ThisString)
{
let counter=0;
for (let i=0; i<ThisString.length; i++)
{
if (ThisString[i] == “B”)
{
counter+=1;
}
}
return counter;
}

console.log (countBs(“teBstBBgub”));

To be able to test for a value that is passed to the functiona along with the string is simple as follows -
function countBs(ThisString,LetterToCheck)
{
let counter=0;
for (let i=0; i<ThisString.length; i++)
{
if (ThisString[i] == LetterToCheck)
{
counter+=1;
}
}
return counter;
}

console.log (countBs(“teBstBBgub”,“e”));

#142

MINIMUM

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

RECURSION

function isEven(N) {
if (N < 0) {
N = N * (-1);
}
if (N == 0) {
return console.log(“Even”);
}
if (N == 1) {
return console.log(“Odd”);
}
else {
while (N > 1) {
N = N - 2;
}
}
isEven(N);
}

BEAN COUNTING

function countChar(string, letter) {
let result = 0;
let length = string.length;
while (length > 0) {
if (string[length - 1] == letter) {
result = result + 1;
}
length = length - 1;
}
return result;
}