Chapter 3 Exercises

Okay. I have logged out of the Academy for today, but I will attempt the exercise tomorrow. Hope you don’t mind. Thank you!

Minimum

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

Recursion

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

Bean Counting

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

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

Here is my tentative answer to #3, the Bean Counting exercise. I had to refer to other students’ posts to get an idea of how the problem needed to be set up.

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

function countBs(str) {
   return countChar(str, "B");
}
console.log(countBs("Total number of beans"))

The result in my console came out 0. What did I do incorrectly?

Thank you!

I dont see were are the exercises :slightly_frowning_face:

@jgentry, The function only counts capital letter B’s. As thats what you have provided. Provide small letter “b” so that your function will count it.

Hi @Lacho, Please scroll on top of the page to find the questions link that Ivan has provided. You will find the question prompts there.

Thanks.

1 Like

That’s not a T, it’s an I.

I barely understood what you were saying. Please discuss more clearly.

Thank you.

1 Like

There was a typo. Please check my response again. I have edited the text.

1 Like

Okay. I think I can handle it now. I’ll give it another go.

Thanks!

1 Like

I got 2 in my console. Did I do something incorrectly, or is that the result expected?

1 Like
//exercise minimum
function min(a, b) {
  if (a < b) return a;
  else return b;
}
1 Like
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));

console.log(isEven(75));

console.log(isEven(-1));
1 Like
   Minimum Exercise
  var min = function(k,d){
    return (k < d ? k : d);
  }

   Recursion Exercise
   function even(number) 
  { if (number == 0) return true;
    else if (number == 1) return false;
    else if (number < 0) return even(-n);
    else return even(number - 2);}


   Bean Counting Exercise
  var count = function(str,char){
    var beans = 0;
    if (typeof(str) !== "string" ) return 0;
    for(var number=0;number<str.length;number++){
      if(str[number]===char[0]) beans++;
    }
    return beans;
  };
  var countBeans = function(str){
    return count(str,"B");
  };
1 Like

Chapter 3 Exercises:
Minimum

<script>

      function min(param1, param2){
            //if param2 is undefined (he refers to this as "edge case")
            if (param2 === undefined){
                  //return param1
                  return param1;
            }
            //if param1 is less than param2
            if (param1 < param2) {
                  return param1;
            }
            
            else {
                  return param2;
            }

      }

      console.log(min(0,10));
      //0
      console.log(min(0, -10));
      // -10
      console.log(min(4));
      //4
      console.log(min(5,5));
      //5
     
      </script>

Recursion

<script>

      function isEven(n) {
            //to deal with the minus 1 (edge case)
            if (n < 0) {
                  n = Math.abs(n);  //absolute value
            }

            //base case follows, if n is zero, return true
            if (n === 0) {
                  return true;
            }

            //if n is one, return false
            if (n ===1) {
                  return false;
            } //base case ends

            //in all other cases, apply isEven to n minus two - recursive case
            return isEven (n - 2);
                  }

      console.log(isEven(50));
      //true
      console.log(isEven(75));
      //false
      console.log(isEven(-1));
      //false
      console.log(isEven(-14));
      //true
      
      </script>

Bean Counting (1)

<script>
//part 1 of 2
      function countBs(string) {
            //create a result, set it to zero
            let result = 0;
            //loop over the string
            for(let i=0; i<string.length; i++) {
      //if current character is a "B"
            if (string[i] === 'B') {
                  //we want to increment our result by one
                  result +=1; //result++ OR result = result + 1 (all the same)

            }
      }
      //return result
      return result;
      }

      function countChar(string, character) { //this is to get the "k" below
            //create a result, set to zero
            let result = 0;
            //loop over the string
            for(let i=0; i<string.length; i++) {
      //if current character matches input character
            if (string[i] === character) { //he removed the single quotes
                  //we want to increment our result by one
                  result +=1; //result++ OR result = result + 1 (all the same)

            }
      }
      //return result
      return result;

      }

      console.log(countBs("BBBBBC"));
      // 5
      console.log(countChar("kakkerlakkk", "k"));
      // 6
           
      </script>

Bean Counting (2)

<script>
      //part 2 of 2 - answer to second part of question
      function countBs(string) {
            //return a call to countChar with input and "B"
            return countChar(string, 'B');

      }


      function countChar(string, character) { //this is to get the "k" below
            //create a result, set to zero
            let result = 0;
            //loop over the string
            for(let i=0; i<string.length; i++) {
      //if current character matches input character
            if (string[i] === character) { //he removed the single quotes
                  //we want to increment our result by one
                  result +=1; //result++ OR result = result + 1 (all the same)

            }
      }
      //return result
      return result;

      }

      console.log(countBs("BBBBC"));
      // 4
      console.log(countChar("kakkerlakkk", "a"));
      // 2
     
     
      </script>

I did have help from internet, YouTube, previous answers from students…this is pretty humbling.

1 Like

Minimum

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

else return b;
}
console.log(min(0, 10));
// → 0
console.log(min(0, -10));
// → -10

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

1 Like

3.1 Minimum

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

3.2 Recursion
Same result, but without recursion

function isEven(num){
  if(num%2==0) return true;
  else if (num == -1) return "??"; 
  else return false;
}  

3.3 Bean counting

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

function countChar(str, value){
  let count = 0;
  for(let x = 0; x < str.length; x++){
    if(str[x] === value) count += 1; 
  }  
  return count;  
}  
2 Likes

Minimum:

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

1 Like

function isEven(N){
if (N==0){
document.write(“even”)
}
else if (N==1){
document.write(“odd”)
}
else if(N<0){
return isEven(-N)
}
else{
return isEven(N-2)
}
}

1 Like

Bean counter

function countChar(string,x) {
      let count = 0;
      for (let N=0; N<string.length; N++){
        if (string[N]==x) {
          count++;
        }
      }
      document.write(count)
    }
1 Like
1. Minimum:
function min(a,b){
        if(a>b){
          return(b);
        }else{
          return(a);
        }
      }
        var answer = min(12,14);
        document.write(answer + "<br>")
  1. isEven
function isEven(a){
  if(a == 0){
    return ("True")
  }else if(a == 1){
    return ("False")
}else if (a < 0){
    return(isEven(-a))
}
  else{
    return (isEven(a -2))
  }

}
document.write(isEven(-1))
</html>
function countBs(string){
  let count = 0
  for (let i = 0; i < string.length; i++){
    if(string[i] == "B"){
      count++
    }
  } return (count);
}
  document.write(countBs("TheQuickBrownFoxWasVeryBBBad"))
// 4

PART 2:

function countChar(string, char){
  let count = 0
  for (let i = 0; i < string.length; i++){
    if(string[i] == char){
      count++
    }
  } return (count);
}
  document.write(countChar("TheQuickBrownFoxWasVeryBBBad", "e"))
// return 2
1 Like

//Minimum Function

function mn(n1,n2){if(n1<n2) return n1; else return n2;}

console.log (mn(-50,3))

//Recursion
function is_even_or_odd(n) {
if (n == 0) return true;
else if (n == 1) return false;
else if (n < 0) return is_even_or_odd(-n);
else return is_even_or_odd(n - 2);
}

console.log(is_even_or_odd(20))
console.log(is_even_or_odd(85))
console.log(is_even_or_odd(-93))
console.log(is_even_or_odd(-30))

//Bean Counting

function charcount(string, ch) {
let tc = 0;
for (let i = 0; i < string.length; i++) {
if (string[i] == ch) {
tc += 1;
}
}
return tc;
}
function countBs(string) {
return charcount(string, “B”);
}
console.log(countBs(“Big Burger with Butter”));
console.log(charcount(“karaoke kills singing skills”, “k”));

1 Like