Chapter 3 Exercises

Chapter 3 exercises

function getMin(x, y){
          var minimum = Math.min(x,y);
          return minimum;
        }
// Test
var answer = getMin(5, 10);
alert(answer);

RECURSION

function isEven(number){
var value = false;
if(number % 2 == 0){
      value = true;
    }
      return value;
}
// Test 1
var answer = isEven(4);
alert(answer);

// Test 2
answer = isEven(5);
alert(answer);

BEAN COUNTING

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

// FUNCTION TWO
function countChar(string, letter){
var toReturn = 0;
for(var i = 0; i < string.length; i++){
      if(string[i] == letter){
         toReturn++;
       }
    }
return toReturn;
}
//Function 1 - test
var num = countBs("BaBandtBandBTwo");
alert(num);
1 Like
  1. function minimum(x,y){
    if(x<=y){
    return x;
    }
    else return y;
    }
var answer = minimum(-2,5);
alert(answer);

(Returns “-2”)

  1. function isEven(x){
    

    function findEven(current){

    if (current==0){
    return true;
    }
    else if (current==1){
    return false;
    }
    else if (current<0){
    return findEven(current+2);
    }
    else{
    return findEven(current-2);
    }
    }
    return findEven(x);
    }
    var answer = isEven(-1);

     alert(answer);
    

(Returns “false”)

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

(Returns 2)

function countChar(string,Char){
let counted = 0;
for(let n = 0; i < string.length; n++)
{
if (string[n]== Char)
{
counted +=1;
}
}
return counted; } console.log(countChar(“lalaland”, “l”));

(Returns 3)

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

Minimum:
var a = Math.min(5, 10);
console.log(a);

Recursion:
var isEven = function(num) {
if (num == 0)
return true;
else if (num == 1)
return false;
else {
num = num - 2;
return isEven(num);
}
}
console.log(isEven(75));

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

var count = countBs(“BlisteringBellyfat”);
alert(count);

count = countChar(“SlipperySistersAtSchool”, “S”);
alert(count);

1 Like

My recursion even/odd function, call and user error detection:

        function isEven(x){
          if(x==0){return "even";}
          if(x==1){return "odd";}
          else{return isEven(x-2);}
        }

        let a=prompt("Give me any positive number:");
        while(a<0){
          a=prompt("Give me any POSITIVE number:");
        }
        document.write(a + " is an " + (isEven(a)) + " number");
1 Like
// 1. 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

        // Second solution to minimum

        var a = Math.min(5, 10);

        console.log(a);

        

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

            }

            console.log(isEven(58));


            console.log(isEven(77));


            console.log(isEven(-1));


           

        // 3. 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("Barbecue"));

        console.log(countChar("Test", "t"));

1 Like
**Minimum**

var min = function (a,b) {
return (a <= b) ? a : b;
};

console.log(min(0, 15));
console.log(min(0, -20));
console.log(min(5, 5));

>  0
>  -20
>  5


**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(word,character) {
        var sum=0;
        for(i=0;i<word.length;i++) {
          if(word[i]==character) {
            sum++;
          }
        }
        return sum;
      }



*// countBs function definition*
      
function countBs(text) {
        return countChar(text,"B");
      }

// Example of application
      console.log(countBs("BarBaraBerryBoyBlueBox"));
   > 6
1 Like

Hi everybody!

I need some help over here! :thinking:

I’m doing the Recursion Exercise and don’t understand it :frowning_face:
Why parameter is equal to zero, one and less than…

Hope can anyone help me! :blush:

Hi @alejandravisa,

Check out this post by carlos - Chapter 3 Exercises .

Hope this helps. :slight_smile:

Minimum:

let min = function(a,b){
return (a < b ? a : b)
};

Recursion:

let abs = x => x<0 ? -x : x
let isEven = function(a){
return (a === 0 ? true : (abs(a)===1 ? false : isEven( abs(a)-2 )))
};

Bean Counting

let countChar = function(str,char){
let Bs = 0;
if (typeof(str) !== "string" ) return 0;
for (let i=0;i<str.length;i++){
if(str[i]===char[0] Bs++;
}
return Bs;
};
let countBs = function(str){
return countChar(str, "B");
};

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 (x) { if(x%2==0) return true; else if (x==1) return false; else return isEven (x-2); } console.log(isEven(50)); console.log(isEven(75)); console.log(is Even(-1));

Bean Counting

function countChar(string, ch) {
var counted = 0;
for (var i = 0; i < string.length; i++)
if (string.charAt(i) == ch)
counted += 1;
return counted;
}

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

2 Likes

Thanks a lot @Malik!
I didn’t know that “else statement” can create a loop.

1 Like

Minimum number

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

Recursion

  function isEven(N){

         if (N ==0){
             alert("the number is even");}
         else if (N==1){alert("the number is odd"); }
         else if(N<0){isEven(N+2);}
         else isEven(N-2);
         }
isEven(-10);


Bean counting

function countBs(str){
 var count=0;
    for (x=-1;x<str.length;x++){

    if (str.charAt(x)=="B"){count=count+1};

  }
  alert (count);
  };

  function countChar(str,N){
 var count=0;
    for (x=-1;x<str.length;x++){

    if (str.charAt(x)==N){count=count+1};

  }
  alert (count);
  };
countBs("string");
countChar("string","N");
2 Likes
  1. Min Function
    function min(a, b) {
    if (a<b) return a;
    else return b;
    }
  2. isEven Function for (50,75,-1)
    function isEven(n) {
    //50 if(n==0) return true;
    //75 else if(n==1) return false;
    // -1 else if (n<0) return isEven(-n);
    else return isEven (n-2);
    } //should return false
  3. Bean Counting (A and B function)
    function countChar(string, ch) {
    let count = 0;
    //for loop
    for(let i =0; i<string.length; i++) {
    //if statement
    if(string[i] == ch) {
    count+=1;
    }
    }
    return count;
    }
    function countBs(string) {
    return countChar(string, “B”);
    }
2 Likes
  1. Minimum
 function min(a, b) {
    if (b === undefined) {
        return a;
    } 
    if (a < b) {
        return a;
    } else {
        return b;
    }
}
console.log(min(0, 10));
console.log(min(0, -10))
  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);
}
console.log(isEven(50));
console.log(isEven(75));
console.log(isEven(-11));
  1. 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"));
 console.log(countChar("kakkerlak", "k"));
2 Likes

Minimum
function minNum (x , y){
if (x < y) {
return x;} else {
return y;}}
console.log(minNum (4, 5));

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

1 Like
  1. Minmum:

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

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

  3. 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”);
    }

2 Likes

wow, great excercises!

I had to read the 3rd excercise for about a thousand times and it only became clear after googling for a better explanation of how to get the nth character of a string to solve it.
If someone else has the same problem, this thread helps: https://stackoverflow.com/questions/4282798/how-to-get-nth-character-in-a-string-in-javascript

Minimum

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

console.log(min(3,6));

Recursion

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

console.log(isEven(23));

Bean Counting:

function countBs(str, character)
{
var counter=0;
for(len=str.length-1;len>-1;len=len-1)

	if(len>=0)
	{
		if(str[len]==character)
		{
			counter=counter+1;
		}	
		else{}
	}
return counter;
}

console.log(countBs("ooa","a"));
2 Likes

Yeah, that was pretty tough. I got number 1 on my own pretty easy and quickly. 2, I was using n =… instead of n ==… , once I figured figured that out it wasn’t too bad. 3, I couldn’t get to save my life. I was prompting for a name, turing it into an array. Then i wanted to pop all characters that aren’t “B” or the desired " ". But i couldn’t get that to work as desired no matter how much i looked it up. I would have deleted all not “B”, then counted the objects remaining. Heres number 1 since thats all i completed on my own.

Minimum

function minimum(a,b){ if (a < b){ return a; } else{ return b }
         }
         console.log(minimum(25,7))
        </script>
        <h3>Minimum complete</h3>
1 Like

Ok, I redid this assignment after reading ch2 and 3 more thoroughly. It went much better. I was successful on the first 2 problems. (1st was identical code)(2nd, mine was less elegant). I wrote code that made sense to me, but it wasn’t successfully looping, and I can’t figure out why. Can someone please help show me where I messed up? Because I don’t see the difference. Unless its as simple as he used “string” and i used “word” mine would always catch the first b or k but not loop. Thanks

//mine
function countChar(word , ch){
let count = 0;
for (n = 0 ; n < word.length ; n++){;
if ( word[n] == ch){;
return count += 1;
}
}
return count;
}
//elo js
function countChar(string, ch) {
let counted = 0;
for (let i = 0; i < string.length; i++) {
if (string[i] == ch) {
counted += 1;
}
}
return counted;
}

1 Like