Chapter 3 Exercises

Hi @andylukepd,

Instead of looping your counter, I would advise you to simply check the divisibility of the number and then return true or false. It is way simpler this way. Please check the code that I have given below –

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

Hope this helps. :slight_smile:

1 Like
  1. Minimum

    function min (x,y){
      if (x==y) 
        document.write ("false");
      else if (x<y)
    	document.write (x);
      else
     	document.write (y);
     }    
    
  2. Recursion

    function isEven(n){
       if (n==0)
          document.write ("true");
       else if (n<0)
          return isEven (-n);
       else if (n==1)
          document.write ("false");
       else
          return isEven (n-2);   
    }
    
  3. Bean Counting

3.1. Count Bs

 function countBs (string) {
     var numBs = 0;
     for (position=0; position < string.length; position++){
         if (string[position] === “B”){
             numBs = numBs + 1
         }
     }
     return numBs
 }

3.2. Count Characters

 function countChar (string, char) {
     var numChars = 0;
     for (position=0; position < string.length; position++){
         if (string[position] === char){
            numChars = numChars + 1;
         }
     }
     return numChars;
 }
1 Like
  1. Minimum

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

  1. Recursion

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

  1. Bean Counting

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

function countBs(str) {
return countChar(str, “A”);
}

1 Like

Can someone please tell me how to easily paste code that is formatted so it appears nicely as in some of the other posts?

1 Like

Chapter 3
**Minimum Exercise **
function min(param1, param2) {
if (param2 === undefined){
return param1;
}
if (param1 < param2) {
return param1;
}
else {
return param2;
}
}
console.log (min(10,2));
console.log (min(-2,2));
console.log (min(2,2));
console.log (min(10));

**Recursion Exercise **

function isEven(n) {
if (n < 0) {
//Make n not negative
n = -n;
}
//if n is 0
if (n === 0) {
//return true
return true;
}
if (n === 1){
//if n is 1
//return false
return false;
}
return isEven(n -2);
// in all other cases, apply isEven to n minus 2
}

console.log(isEven(50));
console.log(isEven(75));
console.log(isEven(-1));

Bean counting Exercise

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

function countChar(string, character) {
//crate a result, set to 0
let result = 0;
//loop over the string
for(let i = 0; i < string.length; i++){
//if the current character is a “b”
if (string[i] === character){
//increment our result by 1
result = result + 1;
}
}
//reutrn result;
return result;
}
console.log(countBs(“BBC”));
console.log(countChar(“kakkerlakakak”, “k”));

1 Like

Check this guide :nerd_face:

Carlos Z

1 Like

Hi guys, I’ve struggled a bit with these exercises, especially with ‘recursion’ and ‘bean counting’. So far I’ve been understanding the fundamentals, but still find it a bit hard to organize my code structure and thinking about these abstract solutions.

Any advice will be greatly appreciated!

    <script>
    // find the min number
    
    function minNumber(a,b) {

      if (a < b) {
        console.log(a);
      }
      else {
        console.log(b);
      };
    };

    minNumber(8,6);
    </script>
    <script>
    // recursion

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

      } else {
        return isEven(number - 2);
      }
    }

    console.log(isEven(5));
    </script>
    <script>
    // bean counting

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

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

    console.log(countChar("the Bword"));

    console.log(countBs("More BBBs!", "B"));
    </script>
1 Like

// find minimum

function minimum(a,b){

  if (a < b){



    return a;
  }

  else {


    return b;
  }

}

alert(minimum(9,1));


// recursion

function even_num(n){
  if(n == 0) return true;
  if(n == 1) return false;
  if( n < 0) return "Negative integer";
  else return even_num(n-2);




}


console.log(even_num(50));
console.log(even_num(75));
console.log(even_num(-1));


// counting beans


function countBs(string){

  return countChar(string, "B");
  // creat count and set to 0;

 }

 function countChar(string, character){

   count = 0;
   for(i = 0; i < string.length; i++){

     if(string[i] == character){
       count++;
     }
   }

  return count;
 }



console.log(countBs("ABBIBKAF"));
console.log(countChar("KKkAbcFIA", "A"));
1 Like

Hi!
Try pressing the ‘space bar’ at least four times before each line. The code should apear nicely. But you still have to indent it manually. Hope it works!

Ch 3 Ex 1

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

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

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

1 Like

The idea of using loops is starting to make sense. The way to call a function to do another function is interesting, yet challenging. I am still pushing forward to complete the course no matter what.
Minimum
function min(a,b){
if (a<b)
return a;
else return b;
}
document.write(min(20,13));
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));
console.log(isEven(75));
console.log(isEven(-2));

Bean Counting
function countBs(string){
return countChar(string, ‘B’);

}

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

}

}
return result;
}

console.log(countBs(“BBBBBBBBBBBBBBBBBBBB”));
console.log(countChar(“lkdsjaflajdaaaaaaj”, “a”));

2 Likes

Thanks for the detailed explanation.

Exercise 1 - Minimum:

<meta charset="utf-8">

<title>This is a great website</title>
<h1> Exercise 1 - Minimum </h1>

<script>

    function min (x, y){

    if (x < y)

      return x;

  else
    return y;



    }

console.log(min(2,4))

</script>

Exercise 2 - Recursion:

<meta charset="utf-8">

<title>This is a great website</title>
<h1> Exercise 2 - Recursion </h1>

<script>

function isEven (n) {

  if (n == 0){
  return true;

}
else if (n == -1){

  return false;

}
else {return isEven (n - 2)}

}

console.log (isEven(75));

</script>

Exercise 3 - Been Counting (Had trouble with this one :confused: )

<meta charset="utf-8">

<title>This is a great website</title>
<h1> Exercise 2 - Recursion </h1>

<script>

function countChar(string, character) {
  let counter = 0

  for (var wordCount = 0; wordCount < string.length; wordCount++){

    if (string[wordCount] == character)

    counter += 1;
  }

  console.log (counter);
}

function countBs(string)
{

  return countChar(string, 'B')

}

countBs("Banana")



</script>
1 Like
function countBs(x){
    var y=0; 
  for(tracker=0 ;tracker<=x.length; tracker++){
      let toReturn= x[tracker];
      if(toReturn!="B"){console.log();}
      else{(y += toReturn.length)};
    }
    return y;
}
countBs("BigBenBrownBeansbbbBBB");

/* Rewriting the countBs function*/
function countChar(x,z){
    var y=0; 
  for(tracker=0 ;tracker<=x.length; tracker++){
      let toReturn= x[tracker]; 
      if(toReturn!=z){console.log();}
      else{(y += toReturn.length)};
    }
    return `${y} ${z}`;
}
countChar("BigBenBrownBeansbbbBBB","B");
1 Like

//
//minimum
//
function minTest2Numbers(enterFirst,enterSecond) {
alert(“Min of " + enterFirst +”, “+ enterSecond +” is " +
Math.min(enterFirst,enterSecond));

  }
   minTest2Numbers(5,10);

//isEven
function isEven(enterWholeNbr) {
if (enterWholeNbr < 0) {
enterWholeNbr = Math.abs(enterWholeNbr);
}

        if (enterWholeNbr ===0) {
              console.log("is " +  enterWholeNbr + " even? true")
            }

        if  (enterWholeNbr ==1) {
              console.log("is " +  enterWholeNbr + " even? false")
              }

              else {
                enterWholeNbr = enterWholeNbr -2;
                console.log("is " +  enterWholeNbr + " even? false")
              }

            }
            console.log(isEven(75))
          // isEven(3);

//
//BeanCounting countBs
//
function countBs(enterString) {
var count = 0;
var countUpper = “B”;

      for (i = 0; i < enterString.length; i++) {
        console.log(enterString[i]);

     }
     return count;

    }
    console.log(countBs("Bye Good Bye"));
    //console.log(count);

//
//BeanCounting countChar
//
function countChar(enterString, enterLetter) {
var count = 0;
var Letter =enterLetter;

      for (var i = 0; i < enterString.length; i++) {
        if (Letter.indexOf(enterString[i]) > -1) {
          count++;
        }
      }
      return "This string  has, " + count + " "+ enterLetter + " Letter(s)";

  }
  //countChar("ABCDBBBBB", "B");
  console.log(countChar("ABCDBBBBB", "A"));
1 Like

Sorry I do not have any advice but just letting you know you are not alone. These exercises are very hard for someone without any previous knowledge. Let’s keep trying!!

1 Like

Function 1 Math Minimum Recreation:

function returnMin( a, b, c)
{
    if(a < b && a < c)
    {
        console.log(a);
    }
    else if(b < a && b < c)
    {
        console.log(b);
    }
    else if (c < a && c < b)
    {
        console.log(c);
    }
    else console.log("Error Give me numbers that are not equal to each other!");

}

Function 2: Recursion

function isEven(number)
{
    if(number == 0)
    {
        console.log( " this number is even ");
        return true;
    }
    else if (number == 1)
    {
        console.log( " this number is odd ya dingus ");
        return false
    }
    else if (number < 0)
    {
        console.log(number + "Will be changed to a positive inteager so your number can be checked ");
        number = -number;
        return isEven(number);
    }
    else

Function 3: Bean Counting

var capitalBs = 0;

function countBs(text)
{
    for(counter = 0; counter <= text.length; counter++)
    {
        if(text[counter] == "B")
        {
            capitalBs++;
        }
    }
}
// Idk why but when this function is executed without being used by countBs it only ever returns 1.
function countChars(text, char)
{

let charAmmt = 0;

    for(counter = 0; counter <= text.length; counter++)
    {
        if(text[counter] == char)
        {
            charAmmt++;
        }
    }
    return (charAmmt);
}

countChars("NeverGonnaGiveUup");

function countBs(text)
{
    return countChars(text, "B");
}

countBs("Boy Being Blue");
1 Like

Thanks for your encouragement Crypila, much appreciated!

Hope our efforts will pay off soon :nerd_face:

1 Like

MINIMUM

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

document.write (min (0, 20));

RECURSION

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

console.log(isEven (50));
console.log(isEven (75));
console.log(isEven (-1));

BEAN COUNTING

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

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

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

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