Chapter 3 Exercises


#163

**Minimum:

function minValue(a,b){
if (a > b ){
return b;}
else return a
}
console.log(minValue(7,8));

Recusion:

function isEven(N){
if ((N-2)%2 == 0) {
return “even”;
} else {
return “odd”;}
}

console.log(isEven(-1));

Bean Counting:

function countBs(text) {
counter = 0;
for(letter = 0; letter < text.length; letter++) {
if(text[letter] == “B”) {
counter++;
}
}
return counter;
}
console.log(countBs(“Boys From Brazil”));
// → 2

function countChar(text, char) {
counter = 0;
for(letter = 0; letter < text.length; letter++) {
if(text[letter] == char) {
counter++;
}
}
return counter;
}
console.log(countChar(“tottenham hotspurs”, “t”));


#164

Chapter 3 Exercise Solutions:

Click to show solutions
<!DOCTYPE html>
<html>
  <head>
    <title>Exercise!</title>
  </head>
  <body>
    <script type="text/javascript">
      let min = (a, b) => a < b ? a : b;
      function isEven(number) {
        if (number == 0) {
          return true;
        } else if (number == 1) {
          return false;
        }
        return isEven(Math.abs(number - 2));
      }
      function countChar(str, c) {
        let b = 0;
        for(let a = 0; a < str.length; a++) {
          if (str[a] == c)
            b++;
        }
        return b;
      }
      function countBs(str) {
        return countChar(str, 'B');
      }
      window.onload = function() {
        console.log("min(6, 0): " + min(6, 0));
        console.log("min(-1, 1): " + min(-1, 1));
        var array = [-2, -1, 0, 1, 2, 3, 4, 50, 75];
        array.forEach(function(number) {
          console.log("isEven Test: number=" + number  +
                      " isEven=" + (isEven(number) ? "True" : "False"));
        });
        var array2 = [["Begin", "n"], ["Blockchain", "o"], ["Revolution", "w"]];
        array2.forEach(function(arr) {
            console.log(`countBs Test: countBs(${arr[0]}) = ${countBs(arr[0])}`);
            console.log(`countChar Test: countChar(${arr[0]}, ${arr[1]}) = ${countChar(arr[0], arr[1])}`);
        });
      };
    </script>
  </body>
</html>

#165

Minimum:

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

else
return b;
}

Recursion:

function even(x){
if (x === 0)
return “even”;

else if (x === 1)
return “odd”;

else if (x < 0)
return even(-x);

else
return even(x - 2);
}

Bean counting:

  1. function countB(str){
    let b = 0;
    for (count = 0; count <= str.length; count++){
    if (str[count] == “B”)
    b++;
    }
    return b;
    }

  2. function countChar(str,char){
    let er = 0;
    for (count = 0; count <= str.length; count++){
    if (str[count] == char)
    er++;
    }
    return er;
    }


#166

Minimum

var a = Number(prompt("Low value of the range"));

var b = Number(prompt("High value of the range"));

function min(a,b){

if(a<b)

return a;

else {

return b;

}

document.write(min(a,b));

Recursion

function isEven(num) {

if (num < 0) num = -num; // remove negative sign

switch (num) {

case 0: return true;

case 1: return false;

default: return isEven(num-2);

}

}

Bean Counter

function countChar(str,chr)

{

let count = 0;

for (let i=0; i<str.length; i++){

if (str[i]==chr)

count++;

}

return count;

}

function countBs(str)

{

return countChar(str, ‘B’);

}


#167

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

function isEven(number) {

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

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

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


#168

Function 1

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

}
document.write(min(8,4))

FUNCTION 2

function isEVEN(x){
  //defend against negative numbers
  if (x<0){
    x=x*-1
  }

  while ( x > 1){
  x -=2}

  if (x==0){
    return true;
  }else {
    return false;
  }
}

document.write(isEVEN(101))

Function 3

function countBs(inputString){
  let L = inputString.length
  var t = 0
  var successB = 0

  while(t<L){
      if(inputString[t] == "B"){
        successB += 1;
        }
      t +=1;
    }
    return successB
  }


function countChar(inputString,characterFinder){
  let L = inputString.length
  var t = 0
  var successB = 0

  while(t<L){
      if(inputString[t] == characterFinder){
        successB += 1;
        }
      t +=1;
    }
    return successB
  }
document.write("Function countB: " + countBs("Boy oh Bboy oh BOY") + "<BR>");
document.write("Function countChar: " + countChar("Boy oh Bboy oh BOy","h"));

#169

Minimum

function smallest(x , y) {
	if (x < y ) return x;
	if (x == y) return "they are the same";
	else return y;
}

Recursion

function isEven (x) {
if (x == 0)  { 
	return true;
} else if (x == 1) {
	return false;
} else if(x < 0 ) {
	return "Please provide a positive whole number"
}
return isEven(x - 2);

}


#171

Minimum of two numbers.
function smallest(a,b) {
if (a - b > 0 ) {
console.log(b);
}
else if (b - a > 0) {
console.log(a);
} else if (a - b == 0)
console.log(“they are the same”);
}
smallest(10,9);

isEven using a while loop (it can handle a bigger number than recursion).
function isEven(N) {if (N < 0) {console.log(N + " is not a positive interger!")};
{ while (N > 1) {N= N - 2};
if (N == 0 ) { console.log(“Even”);}
if (N == 1) {console.log(“Odd”);}}}
isEven(99999999999)

isEven using a recursive function.
function isEven(N)
{if (N <= 0) {console.log(N + " is not a positive interger!");}
else if (N == 2 ) {console.log(true);}
else if (N == 1) {console.log(false);}
else if (N = N - 2) {isEven(N)};}
isEven(99999)

Bean counter - I had to get help for this one in the forum, I made the logic way too complex and got in a mess.
function countletters (str,target) {
var count = 0;
for (var i = 0; i < str.length; i++) {
if(str.charAt(i) == target) { count++ }
}
return count;
}
console.log (countletters(“cbcbcbcbcb”,“c”));


#172

Exercise 1, “minimum”.

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

Exercise 2, “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);
}
}

Exercise 3, “bean counting”.

function countChar(aString, aChar) {
let count=0;

for(let i = 0; i<aString.length; i++){	
	if (aString[i] == aChar){
	count++;
	}				
}

return count;
}

function countBs(aString) {
return countChar(aString, “y”);
}
console.log(countBs(“Brygga kaffe med Bryan på en brygga i Berlin BOIII”));


#173

MINIMUM
function minimum(a, b){
if (a < b) { return a;}
else if (a > b) { return b; }
else {
document.write( "eaqual
");
}
}


#174

Recursion

function isEven(n){
if (n == 0) return true;
else if (n == 1) return false;
else if (n < 0) return “negative number!”;
else return isEven(n-2);
}


#175

Counter

function countChar(s, letter){
var n = s.length;
var count = 0;
for( i = 0; i < n; i++){
if(s[i] == letter)count++;
}
return count;
}


#176

Write a function min that takes two arguments and returns their minimum.


if (x &lt; y) return x;

else return y;}

var answer = min (10,50);

document.write (answer); 

Recursion

function isEven(n) {

if (n == 0) return true;

else if (n == 1) return false;

else if (n &lt; 0) return isEven(-n);

else return isEven(n - 2);

}

console.log(isEven(2));

true

console.log(isEven(11));

false

console.log(isEven(-5));

 false

Bean counting

function countChar(string, ch) {

let counted = 0;

for (let i = 0; i &lt; string.length; i++) {

if (string[i] == ch) {

counted += 1;

}

}

return counted;

}

function countBs(string) {

return countChar(string, &quot;B&quot;);

}

console.log(countChar("lllodllo","l"));

console.log(countBs("BBBICTCOIN WILL MOON"));

:tada: :tada: :tada: :tada: :tada:
:tada: :tada: :tada: :tada: :tada:


#177
  1. function minimum(x,y){
    if (x < y) return x;
    else return y;

     }
     console.log(minimum(9,5));
    
  2. var odd = 1;
    var even = 0;
    function isEven(N)

    {

         if((N - 2) % 2 == even)
         {
           return true
         }
         else
         {
           return false
         }
    
       }
       console.log(isEven(50));
    
  3. 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”));
    console.log(countChar(“kakkerlak”, “k”));


#178

1. Function to calculate minimum

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

2. Function to calculate evenness

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

3. Function to calculate number of times some character appears in a given string

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


#179

You didn’t use recursion for isEven function? Try again with recursion.


#180

// Minimum of two numbers
function getmin(a, b) {
  if (a < b) {
    return a
  }
  else if (b < a) {
    return b
  }
  else {
    return ‘tied’
  }
}

getmin (1,1); //tied
getmin (1,2); //1
getmin (2,1); //1
getmin (2,2); //tied

// Using recursion for IsEven
function isEven(numParameter) {
  if (numParameter == 0) {
    return true;
  }
  else if (numParameter == 1) {
    return false;
  }
  else {
    return isEven(numParameter - 2);
  }
}
console.log(isEven(50)); // true
console.log(isEven(75)); // false
console.log(isEven(-50)); // Uncaught RangeError: Maximum call stack size exceeded
console.log(isEven(-75)); // Uncaught RangeError: Maximum call stack size exceeded

function isEven(numParameter) {
  if (numParameter == 0) return true;
    else if (numParameter == 1) return false;
      else if (numParameter == 1) return false;
        else return isEven(numParameter - 2);
}
console.log(isEven(50)); // true
console.log(isEven(75)); // false
console.log(isEven(-50)); // Uncaught RangeError: Maximum call stack size exceeded
console.log(isEven(-75)); // Uncaught RangeError: Maximum call stack size exceeded

function isEven(numParameter) {
  if (numParameter == 0) return true;
    else if (numParameter == 1) return false;
      else if (numParameter < 0) return isEven(-numParameter);
        else return isEven(numParameter - 2);
}
console.log(isEven(50)); // true
console.log(isEven(75)); // false
console.log(isEven(-50)); // true
console.log(isEven(-75)); // false

// countBs
function countBs(strInput) {
  let intCount = 0;
  for (let x = 0; x <= strInput.length-1; x++) {
    if (strInput[x] == “b”) intCount++;
  }
  return intCount;
}
countBs(‘abcdef’); //1
countBs(‘abababa’); //3
countBs(‘bababab’); //4

// countChar
function countChar(strInput, chrInput) {
  let intCount = 0;
  for (let x = 0; x <= strInput.length-1; x++) {
    if (strInput[x] == chrInput) intCount++;
  }
  return intCount;
}
countChar(‘abcdef’, ‘a’); //1
countChar(‘abcdef’, ‘b’); //1
countChar(‘aaabbbb’, ‘a’); //3
countChar(‘aaabbbb’, ‘b’); //4
countChar(‘bbbaaaa’, ‘a’); //4
countChar(‘bbbaaaa’, ‘b’); //3


#181

Minimum

  var x = 10
  var y = 2
  function minimum(x,y){
    if (x<y){
      return x;
    }
    else if (x>y){
      return y;
    }
    else {
      return "Same Number";
    }
  }
    document.write("<h2>Min(" + x + "," + y + "):  " + minimum(x, y) + "<h2>");

Recursion

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

console.log(isEven(-101));

Bean counting

function countBs(_string) {
    let end = _string.length - 1;
    let count = 0;

    for (let i = 0; i <= end; i ++) {
      if (_string[i] == "B") {
        count++;
      }
    }
    return count;
  }

// testing (must be in the html script tag)
  document.write("<h1>" + countBs("BananaBVB") + "</h1>");

  function countLetter(_string, _toBeCounted) {
    let end = _string.length - 1;
    let count = 0;

    for (let i = 0; i <= end; i ++) {
      if (_string[i] == _toBeCounted) {
        count++;
      }
    }
    return count;
  }

// testing (must be in the html script tag)
  document.write("<h1>" + countLetter("BananaBVB", "n") + "</h1>");

#182

I must say that I found these assignments to be very confusing. I could barely make any sense of what the exercise was.

function min(a,b){
var answer = 0;
if (a>=b){var answer=b}
else {answer=a}
return answer;
}

document.write(min(1,3));

function isEven(n){

if (n==0) {return true;}
else if (n==1) {return false;}
else if (n<0) {return “Please provide a positive whole number”}
else return isEven(n-2);

}

document.write("
" + isEven(50));

function countChar(text,char){

var counter=0;
for (a=0;a<=text.length-1;a++){

  if(text[a]==char){
    counter++;  
  }
}
return counter;

}
document.write(countChar(“BBaaaaad”,“a”));


#184

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

function isEven(n){
if (n<0) n = -n;
while (n>=0) {
if (n==0) return true;
if (n==1) return false;
n-=2;
}
}
console.log(isEven(75))

function countChar(text, char) {
let a=0, c=0;
while (a<text.length) {
if (text[a]==char) c++;
a++;
}
return c
}
countChar(“bastBBBrer”, “t”)