Chapter 3 Exercises

  1.  function min(a,b){
              if(a<=b){
               console.log(a,'ist kleiner als',b);
          } else {
               console.log(b,'ist kleiner als',a); 
         }
       }
    console.log(min(5,9)); 
    
  2.  function even(zahl) {
      var zahl = prompt('Zahl');
      if (zahl==0 || zahl%-2==0 ){
     console.log('is even')
    
    } else {
    console.log(zahl,'is odd')
      }
    }
    console.log(even())
    
  3. function countBs(word){
           var n = word.length;
           var letter;
           var count = 0;
    
    
           for( var i = 0 ; i < n  ; i++ ){
    
             letter = word[i];
           
             if(letter == "B" ){count++ ;}
    
           } 
    
           return count;
       }
    
        console.log(countBs('BuBBlegum')); `
    

couldn’ solve the last one alone. Didn’t create a variable n = word.length and find the mistake :woozy_face:

1 Like

A) Minimum

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

B) Recursion

function isEven(number){
if (number == 0) return (“EVEN”);
else if (number == 1) return (“ODD”);
else if (number < 0) return isEven(-number);
else return isEven(number - 2);
}

C) Bean counting

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

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

1 Like

need help with exercise 2 recursion. my answer i had was origionally

function isEven (x){
for (var counter = x; counter<2 ;counter-2){};

if (counter=0) {return (true)};
if (counter=1){return (false)};
}

var answer = isEven(10);
console.log ("The answer is " + answer)

This returned an answer of false regardless of what number i had in as x
when i put if (counter==0) instead of =0 i would get number undefined. I know my solution is clearly wrong but i dont understand why can someone please help :slight_smile:

Minimum:
var a = 1;
var b = 2;
function min(a,b){
if b>a{
return a;
}
else a>b
return b;
}

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

Bean counting:
function countB(string){
for (let count=0; count<string.length; count++){
if (string[count]=“B”){
count = count+1;
}
}
return count;
}

1 Like

Minimum

      function minimum(a,b){
        if(typeof(a) == "number" && typeof(b) == "number"){
          if(a<b) return a;
          else if(b<a) return b;
          else return "These two values are the equal!";
        }
        else return "The values entered are not both numbers!";
      }

Recursion

      function isEven(num){
        if(typeof(num) == "number") {
          if(num == 0) return true;
          else if (num == 1) return false;
          else if (num < 0) return isEven(-num);
          else return isEven(num - 2);
        }
        else return "Input is not a number!"
      }

Bean Counting

      function countLetter(words, letter){
        if(typeof(words) == "string" && typeof(letter) == "string"){
          let counter = 0;
          for(i = 0; i < words.length; i++){
            if(words[i] == letter) counter +=1;
          }
          return counter;
        }
        else return "Inputs are not both strings!"
      }
1 Like

Minimum
function min(a,b) {
if (a<b) return a;
else return b;}
console.log (min (1,5));
// 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);}

Bean Counting
function countBs (string) {
let count=0;
for (let x=0; x<string.length; x++) {
if (string (x) == “B”){
count += 1;}
}
function countChar (string,char); {
let count=0;
for (let x=0; x<string.length; x++) {
string(x) == char;
count += 1;}
}

1 Like

Here are my solutions, if you know a more eloquent to write this, please let me know, as I it is one of my goals to write code in a better way.

Thank you!

1. Minimum

function minimum(a,b){
        if (a<b) return a;
        else return b;
}
console.log(minimum(0,10));
console.log(minimum(0,-10));

2. Recursion

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

3. Bean Counting

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

function countChar(word,char){
        let n=0;
        for (i = 0; i < word.length; i++) {
          if (word[i]==char) n+=1;
        }
        return n;
}
console.log(countBs("BBC"));
console.log(countChar("kakkerlak", "k"));
1 Like
**Minimum**

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

**Recursion**

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


 function isEven(N){
		if (N === 1){
		 return false}
		if (N === 0){
		 return true}
		if (N < 0){
		return isEven(N + 2)}
	else {
	 return isEven (N - 2)
	  };
};
console.log(isEven(-1));


**Bean Counting**

function countBs(string) {
  let result = 0;
  for (let i = 0; i <= string.length - 1; i++) {
    if (string[i] === 'B') {
      result++;
    }
  }
  return result;
}
function countChar(string, character) {
}
console.log(countBs("BBC"));



function countChar(string, character) {
  let result = 0;
  for (let i = 0; i < string.length; i++) {
    if (string[i] === character) {
      result++;
    }
  }
  return result;
  }
console.log(countChar("kakkerlak", "k"));
1 Like

@Malik please help if you can

1 Like

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

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

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

function countChar(string, Char){
let count = 0;
for (let a=0; a<string.length; a++){
if (string(a) == Char){
count +=1;
}
}
return count;
}

1 Like

MINIMUM:

function min(a,b){
		if(a<b) return a;
		else return b;
}
console.log(min(45, 90));
console.log(min(700, -600));
console.log(min(780, 90));
console.log(min(-700, -600));

Recursion:

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

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

Bean Counting:

function countBs(string) {
        let B=0;
        for (w = 0; w < string.length; w++) {
          if (string[w]=="B") B+=1;
        }
        return B;
}

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

console.log(countBs("BBC"));
console.log(countChar("kakkerlak", "k"));
console.log(countBs("Barracuda Barney"));
console.log(countChar("Mississippi", "s"));
1 Like

minimum

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

console.log (min(3,7));
console.log (min(19,2));

isEven

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

beancounter

function countBs(x){
  var y=0
    for (var n =0 ; n<(x.length) ;n++){
      if (x [n]=="B") {y=y+1;}
    }

  return y
}

console.log (countBs ("ABC"))

function countChar(x , z){
  var y=0
    for (var n =0 ; n<(x.length) ;n++){
      if (x [n]==z) {y=y+1;}
    }

  return y
}

console.log (countChar("ABC", "A"))
1 Like

Hey @andylukepd, hope you are well.

I dont understand what are you trying to do with the for loop.
for (var counter = x; counter<2 ;counter-2)
when counter = 10, 10 is greater than 2, 10 minus 2…

Also you are not comparing in the conditionals if (counter=0), where it should be counter==0.

While the idea of the function is to validate the number. Check the results on other students to give you a good idea of it :nerd_face:

Carlos Z

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