Chapter 3 Exercises

@welowy In forum communication, it helps a lot if you use code blocks like this. please take a look at the picture below.

2 Likes

MINIMUM

 function minNumber(a,b){
      if(a<b) return a;
      else return b;
    }
    console.log(minNumber(10,5));

RECURSION

 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);
  }
  console.log(isEven(50));

BEAN COUNTING

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

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

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

Bean counting was the hardest to understand. I knew that to solve the problem a function inside a function with a for loop was gonna be needed. It was just really hard deciphering. Plus didn’t know what square brackets do but I do now and looks like gonna be learning more about them next.

1 Like

@porter In forum communication, it helps a lot if you use code blocks like this. please take a look at the picture below.

2 Likes

Minimum

      function min(a,b){
        if (a>b){
          return(b);
        }else if (a<b){
          return(a);
        }else
      return("The numbers are the same");
    }

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

function countBs(str){
let letterCount = 0;
let bee = “B”
for (i=0; i < str.length; i++){
if (str[i] == bee){
letterCount++;
}
}return letterCount;
}

function countChar(str, char){
  let letterCount = 0;
  for (i=0; i < str.length; i++){
    if (str[i] == char){
      letterCount++;
    }
  }return letterCount;
}
1 Like

Bean Counting

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


    function countChar(str, char){
      let letterCount = 0;
      for (i=0; i < str.length; i++){
        if (str[i] == char){
          letterCount++;
        }
      }return letterCount;
    }
1 Like
  1. Minimum
function min(a, b)
 {if (a < b) return a;
  else return b;}
  1. 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);}
  1. Bean counting
function countChar(string, char) 
{ let counted = 0; 
for (let i = 0; i < string.length; i++) 
{if (string[i] == char)
{counted += 1;}}return counted;}

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

1 Like

Minimum

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

Recursion

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

Bean Counting

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");
}
1 Like

As it was with the Chapter 2 exercises, my answers to the Chapter 3 exercises will likely be in separate posts because I might need to get help. I tried to get help with #1, the Minimum exercise. When no reply came, I decided to just keep trying. And try again I did!

I referred to other students’ replies for assistance and inspiration. Here is my final answer to #1:

#1: Minimum

function minNumber(a,b){
  if(a<b);
  return(a);
}
console.log(minNumber (15, 30));
1 Like
  1. Finding minimum of two numbers:
    const minNum = (num1, num2) => {
    if(num1 < num2){
    return The minimum is ${num1};
    }
    else if(num2 < num1) {
    return The minimum is ${num2};
    }
    else {
    return “They are equal”;
    }
    };

  2. My attempt at recursive odd even function below. I now see from others in the forum, I could have simplified this by using the Math.abs(num) function to handle negative numbers:

const isEven = function(n) {
// base case for even
if(n == 0){
return “The number is even”;
}
// base case for odd
if(n==1 || n == -1) {
return “The number is odd”;
}
// recursion for positive numbers
if (n > 0){
return isEven(n-2);
}
// recursion for negative numbers
if (n < 0){
return isEven(n+2);
}
};

  1. Counting Character in a string:

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

1 Like

RETURN MINIMUM

function min(a, b) {
return Math.min(a, b);
}
console.log(min(10, 20));

1 Like

Greetings @SVenture, Hope you’re doing well. I noticed that you missed out on the other exercise questions namely – Recursion and Bean Counting. Please do try those as well and post it here so that we can help and evaluate it. :grin:

Happy Learning ! :slight_smile:

1 Like

Here is my answer to #2, the Recursion exercise. I had to refer to other students’ replies to find out exactly how I needed to set up the problem. I didn’t want to waste time trying to get assistance. I never know how long waiting for a reply would take me. Sometimes it takes twenty minutes or less, but other times it takes a whole day.

Capture 2

As instructed in the book, I tested the function on 50 and 75. As always with even and odd numbers, 50 came out true and 75 came out false. But this is how the function behaved when I tested it on -1.

Capture 3

A negative number such as -1 will not work with such a function because it is less than both 0 and 1. To fix this, we would have to change this value to a positive whole number, such as 25.

But did I do anything wrong when I tested -1 on the function, or is that the expected result?
Thank you!

P.S. To avoid struggling with Exercise #3 too much, could someone please help me out? I don’t want to wear my brain out. Thank you!

Greetings @jgentry,

We apologise for the late replies. But because of the large number of students, we have our attention diverted towards all the questions asked by them. But we assure you we get your questions answered within 24hrs.

Coming to your question, yes it is expected that stack size will get overloaded. Because the recursion will keep looping because negative number when subtracted will keep giving a larger negative number.

To solve this you need to keep a “if” condition for the input to be lesser than 1, if it is then remove the negative sign and pass it down to the function. Your function would look like this. –

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

}
console.log(isEven(2))

Hope this helps. Happy Learning! :slight_smile:

1 Like

Okay. I’ll try it tomorrow and see if that works. Thanks!

Joanna

Functions

MINIMUM

function min(num1, num2) {
  if (num1 < num2) return num1;
  else return num2;
}

console.log(min(18, 54));
// → 18
console.log(min(-5, -10));
// → -10
console.log(min(6, 0));
// → 0

RECURSION

function isEven(num1) {
  if (num1 == 0) return true;
  else if (num1 == 1) return false;
  else if (num1 < 0) return isEven(-num1);
  else return isEven(num1 - 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("Bed & Breakfast"));
// → 2
console.log(countChar("Commonwealth", "m"));
// → 2        
1 Like

Here is my revised answer for #2, the Recursion exercise.

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

When the function was tested on 2, it came out true, as shown in the console.
50 came out true
75 came out false
-1 came out false

Now I will need assistance with #3, the Bean Counting exercise. I have read posts from other students that the final exercise was a challenge for them. Could you please share with me how I need to set up the problem?

Thanks!

Hi @jgentry, Please give it a try once and paste the code here, we will assist you based on your approach. :slight_smile:

2 Likes

You are talking about #3, the Bean Counting exercise, are you not?

Thanks.

Yes @jgentry, I am talking about Bean exercise.

1 Like