Chapter 3 Exercises

MINIMUM:
function min(a, b) {

// if a is less than b
//return a
if (a < b) return a;

// otherwise return b
else return b;
}

var answer = min( 10, 11 );
alert(answer)

RECURSION:
function isEven(n) {
// if n is negative
if (n < 0) {
n = -n
}

// if n is 1
if (n === 1) {
return false
}

// if n is 0
else if (n === 0) {
return true
}

//in all other cases, apply inEven to n minus 2
else {
return isEven(n - 2)
}
}

console.log(isEven(50));
// → true
console.log(isEven(75));
// → false
console.log(isEven(-1));
// → false

BEAN COUNTING:
function countBs(string) {
// return a call to countChar with input = “B”
return countChar(string, ‘B’);
}

function countChar(string, char) {
// crete result, set to 0
let result = 0;

// loop over the string
for (let i = 0; i < string.length; i++) {
// if current character matches input character
if (string[i] === char)
//increment out result by 1
result = result + 1;
}

//return result
return result;
}

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

I’m having hard time fully understanding Bean Counting exercise. The part we convert “B” into another character doesn’t make sense in my mind…

1 Like
	//Chapter 3 Exercises

	//Minimum
		function minimumBetweenTheTwo(a, b) {
			return Math.min(a,b);
		}
		console.log(minimumBetweenTheTwo(100,101)); //-> 100

	//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(50)); // -> true
		console.log(isEven(75)); // -> false
		console.log(isEven(-1)); // -> false

	//Bean counting

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

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

			console.log(countBs("IshoBoy in the house")); // -> 1
			console.log(countChar("IshObOy in the building", "O")); //-> 2
1 Like
  1. Minimum
    function min(a, b){
    if(a<b) return a;
    else return b;
    }
    console.log(min(1,5));
    console.log(min(10,5));

  2. Recursion
    function isEven(n) {
    if (n == 0) return “even”;
    else if (n == 1) return “odd”;
    else if (n < 0) return isEven(-n);
    else return isEven(n - 2);
    }

console.log(isEven(15));
console.log(isEven(10));

  1. Bean counting
    function countChar(string, ch) {
    let countResult = 0;
    for (let i = 0; i < string.length; i++) {
    if (string[i] == ch) {
    countResult += 1;
    }
    }
    return countResult;
    }

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

console.log(countBs(“BBQ on your Birthday”));

1 Like

MINIMUM

function min(a, b){}
document.write(Math.min(102, 86));

RECURRSION

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

BEAN COUNTING
couldn’t solve

1 Like

//Minimum
function findmin(a, b)
{
return Math.min(a,b);
}
document.write(findmin(100, 1));

//Recursion
function even(n)
{
if (n == 0)
return (==0);
else if (n == 1)
return false (==!0);

else if (n < 0)
{
return even(-n);
}
else return even(n - 2);
}
console.log(even(50));
console.log(even(75));
console.log(even(-1));

//Bean counting

function countChar(string, ch)
{
let count = 0;
for (let n = 0; n < string.length; n++);
{
if (string[n] == ch);
{
count+=1;
}
}
return count;
}
function countBs(string)
{
return countChar(string, “B”);
}
console.log(countBs(“Bob is 50 and Bold”));
console.log(countChar("billy and Bob bounced out));

If you have any issue or doubt, let us know to help you :nerd_face:.
Share your code if you want to, check other students solution so you can have an idea (not copy/paste, just look and learn from it).

Carlos Z.

My solutions:

MINIMUM:

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

RECURSION:

function isEven(num){
  if (Number.isInteger(num)){
    num = Math.abs(num);
    if (num == 1){
      return false;
    }else if (num == 0){
      return true;
    }else{
      return isEven(num-2);
    }
  }
}

BEAN COUNTING:

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

function countChar(txt, cha){
  if (typeof txt == "string" && typeof cha == "string"){
    let numberOfCharacters = 0;
    for (let i = 0; i< txt.length; i++){
      if(txt[i] === cha){
        numberOfCharacters++;
      }
    }
    return numberOfCharacters;
  }
}
1 Like

Minimum

<html>
  <head>
    <title>Minimum</title>
  </head>
  <body>
    <script>
      function min(firstNumber, secondNumber){
        if (firstNumber < secondNumber){
          return firstNumber;
        } else {
          return secondNumber;
        }
      }

      document.write(min(0, 10)+"<br/>");
      document.write(min(0, -10));

    </script>
  </body>
</html>

Recursion

<html>
  <head>
    <title>Recursion</title>
  </head>
  <body>
    <script>
      function isEven(number){
        if (number == 0){
          return true;
        } else if (number == 1){
          return false;
        } else if (number < 0){
            return isEven(number*-1);
        } else {
          return isEven(number - 2);
        }
      }

      document.write(isEven(50)+"<br/>");
      document.write(isEven(75)+"<br/>");
      document.write(isEven(-1)+"<br/>");

    </script>
  </body>
</html>

Bean Counting

<html>
  <head>
    <title>Bean Counting</title>
  </head>
  <body>
    <script>
      function countChar(letters, character){
        let count = 0;
        for(let char = 0; char < letters.length; char += 1){
          if (letters[char] == character){
            count += 1;
          }
        }
        return count;
      }

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

      document.write(countBs("BBC")+"<br/>");
      document.write(countChar("kakkerlak", "k"));

    </script>
  </body>
</html>
1.

function min(a, b){
return Math.min(a,b)
}

2.

function isEven(x){
if(x % 2 == 0){
return true;
}else {
return false;
}
}

3a.

function countBs(word){
let letter;
let count= 0;
for(let i = 0; i < word.length; i++){
letter = word[i];
if( letter == ‘B’){
count++;
} else {
return count;
}
}

3b.

function countChar(word, ch){
let counted = 0;
for(let i =0; i < word.length; i++){
if(word[i] == ch){
counted++;
}
}
return counted
}

1 Like

Solution to Minimum Exercise:

function minimum (val1,val2) { if(val2 === undefined) { return val1; } if(val1 < val2) { return val1; } if(val1 == val2) { return val1; } else { return val2; } } console.log(minimum(8,)); console.log(minimum(6,7)); console.log(minimum(5,5)); console.log(minimum(4,3));

1 Like

Solution to Recursion Exercise:

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

1 Like

Minimum

function min(x, y) {
if (x < y) return x;
else return y;}
console.log(min(-1,1))

Recursion

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

Beans

(this isn’t my code. It’s from this group) I could not figure out the question, so I worked through this solution). I understand the code, but could not understand the directions. So I give credit to the person who posted this, as it helped me walk through problem solving. Thank You!

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(“Bavarian Beer for Beerfest”));

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(“Pretty Polly Parrot”,“P”));

1 Like

Solution to Bean Counting Exercise:

function countChar(string, character) { let result = 0; for(let N=0; N<=string.length-1; N++) { if(string[N] == character) { result=result+1; } } return result; }

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

console.log(countBs(“BLUBBERINBUBBLEBOY”));
console.log(countChar(“KRISPYKREMEKWIK”,“K”));

1 Like

MINIMUM

function Minimum (a , b) {
  if (a > b){ return min = b;}
  if (a < b){ return min = a;}
  if (a === b) {return min = "Both numbers are equal"; }
 }

alert(Minimum(10000,-10000));

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);
}
alert(isEven(-10))

BEAN COUNTING


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

1 Like

1.MINIMUM
function MinValue(c,d){
if(c>d){return(d);}
if(c<d){return©}
else {return(“none”);}
}

2.RECURSION
function isEven(num){
if(num>0){
if(num == 0){return(true);}
if(num == 1){return(false);}
return(isEven(num-2));
}
else{
if(num == 0){return(true);}
if(num == -1){return(false);}
return(isEven(num+2));

}
}

  1. BEAN COUNTING

function countBs(string){
var n = string.length;
var letter;
var count = 0;

    for( var i = 0 ; i < n  ; i++ ){
        letter = string[i];
        if(letter == "B" ){count++ ;}
      }

      return count;
  }
1 Like

Minimum:

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

Recursion:

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

Bean Counter:

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

function countChar(word, char) {
    let total = 0;
    for(i = 0; i < word.length; i++) {
        if(word[i] == char) {
            total++;
        } 
    }
    return total;
}

function countBs(word) {
    return countChar(word, "B");
}
1 Like

Exercises

Minimum

function min(param1, param2) {
return Math.min(param1, param2);
}

1 Like

Exercises

  1. Minimum

function min(parameter1, parameter2) {
return Math.min(parameter1, parameter2);
}

console.log(min(0, 10));

console.log(min(0, -10));

  1. Recursion

function isEven(n) {

if (n < 0) {

  n = -n
}

if (n === 0) {
return true;
}

if (n === 1) {
return false;

}

return isEven(n - 2);

}

console.log(isEven(50));

console.log(isEven(75));

console.log(isEven(-1));

  1. Bean Counting

function countBs(string) {

  return countChar(string, 'B');
}

function countChar(string, character) {

  let result = 0;
  

  for (let i = 0; i < string.length; i++) {
  
    if (string[i] === character) {
      
      result = result + 1;
    }
  }
  
  
  return result;
}

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

console.log(countChar("kakkerlak", "k"));
2 Likes

I liked your idea of adding a string right before the answer! Makes it seem more “human”

<script>

//MINIMUM
var a = parseInt(prompt("Enter value 1"));
var b = parseInt(prompt("Enter value 2"));
function minimum (a,b){
  if (a>b){
    console.log(b);
  }
  else if (b>a){
    console.log(a);
  }
}

//RECURSION
var even = 0
var odd = 1
var N = parseInt(prompt("Input number!"));
if (N>0){
while ((N >= 1)||(N >= 0)){
  N=N-2;
    if (N==1){
      console.log("The number is odd");
    }
    else if (N==0){
      console.log("The number is even");
    }
}
}
else if (N<0){
while ((N <= 1)||(N <= 0)){
  N=N+2;
    if (N==1){
      console.log("The number is odd");
    }
    else if (N==0){
      console.log("The number is even");
    }
}
}

//BEAN COUNTING
var string = prompt("Enter your string");
var count = 0;
function countBs(string){
  for (N=0; N < string.length; N++){
    if (string[N] === "B"){
      count++
    }
}
console.log(count);
}

countBs(string);

</script>
2 Likes