Chapter 3 Exercises

  • Minumum:
function min( x, y ) {
  if( x < y ) return x;
  else return y;
}
  • Recursion:
function isEven( x ) {
  if( x < 0 ) x = -x;
  if( x == 0 ) return true;
  else if( x == 1 ) return false;
  else return isEven( x - 2 );
}
            
console.log( isEven( 50 ) );
console.log( isEven( 75 ) );
console.log( isEven( -1 ) );
  • Bean counting:
function countBs( str ) {
var count = 0;
for( var index = 0; index < str.length; index++ )
  if( str[ index ] == "B" ) count++;
  return count;
}
            
console.log( countBs( "agBnmB" ) );
            
function countChars( str, c ) {
  var count = 0;
  for( var index = 0; index < str.length; index++ )
    if( str[ index ] == c ) count++;
    return count;
  }
            
console.log( countChars( "agBnmB", "B" ) );
1 Like

Exercise 1: MINIMUM
function min(a, b) {
return Math.min(a, b);
}

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

Exercise 3: BEAN COUNTING
Part 1:
function countBs(x) {
let counts = 0;
for (a = 0; a < x.length; a = a +1) {
if (x[a] == “B”) {
counts = counts + 1;
}
else {
counts = counts + 0;
}
}
return counts;
}
Part 2:
function countChar(x, char) {
let counts = 0;
for (a = 0; a < x.length; a = a +1) {
if (x[a] == char) {
counts = counts + 1;
}
else {
counts = counts + 0;
}
}
return counts;
}

1 Like

1 - Minimum:

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

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

3 - Counting Bean
That took me really a while and I had to look here for some inspiration.

function countBs (string) {
  var countB = 0;
  for (var count = 0; count < string.length; count++) {

    if (string [count] == "B") {
      countB++;
  }
}
return countB;
}
console.log(countBs("MegaBsBsbbBB"));

function countChar (string,text) {
  var countB = 0;
  for (var count = 0; count < string.length; count++) {

    if (string [count] == text) {
      countB++;
}
}
return countB;
}
console.log(countChar("WoWIpockdieNed","W"));
1 Like

Nr. 3.
We all have to look sometimes. :wink:

My old teacher told me that he spent 60% of his time searching https://stackoverflow.com/ when he was programming at work. :man_teacher:

The most important thing is to write the code and understand it, not just copy & paste it. Good luck. :wink:

Ivo

2 Likes

function minimum(a,b){
if(a>b) return b;
if(a<b) return a;
}
var found = minimum(1,100);
alert(found);

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

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

  function countChar(word, letter) {
    var b = 0;
    for(var a = 0; a < word.length; a++){
      if(word[a] == letter) b++;
    }
    return b;
  }
1 Like

//Minimum
function min(a,b){
if(a<b){return a}
else{return b}
}
var toPrint = min(2,5);
console.log(“The smaller number is “+toPrint+””)

//Recursion
function isEven(N){
if(N>=0 && N%2==0){return true;}
if(N>=1 && N%2==1){return false;}
else if(N<0){return “negative”;} //in lack of this line, results less than zero generate “undefined” results
}
console.log(isEven(50))
console.log(isEven(75))
console.log(isEven(-1))

//Bean Counting
function countChar(string, ch) {
let counted = 0;
for (a = 0; a < string.length; a++) {
if (string[a] == ch) {
counted += 1;
}
}
return counted;
}
function countBs(string) {
return countChar(string, “B”);
}
console.log(countBs(“BThis Bshit Baint Bas Beasy Bas Bai Bthought!”));

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

1 Like
  1. Minimum
    image
  2. Recursion
    image
  3. Count Bs
    image
  1. Minimum
    Schermata 2020-04-09 alle 10.47.46

  2. Recursion
    Schermata 2020-04-09 alle 11.35.51

  3. Bean counting

Hi, Is it possible to post the code instead of screenshots… It’ easier to test it then. :roll_eyes:

Ivo

Good point. Will do moving forward…
Toby

1 Like

Use this function of the editor to get syntax highlighting in your code and indentation!

For example:

    function minimum(a, b){
      return (a < b ? a : b )
    }
    console.log(minimum(5,4))
1 Like

Minimum

    function minimum(a, b){
      return (a < b ? a : b )
    }
    console.log(minimum(5,4))

In order to keep this function short, I used the conditional operator ‘?’ here.

isEven

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

My idea to solve this problem for negative numbers was to go up by 2, instead of going down by 2 for positive numbers. Going to 0 is always the right direction!
In the second else ifstatement I just needed to check that number > 0, because if the number is equal to 0, the function will return true. So if the number is neither positive nor 0 it must be negative.

But I also understand that the proposed solution with return isEven(-n) is even better! My program will always run until the last else for negative numbers. But if you turn the sign with -n the number will get postive! Therefore the code of the last else statement will be executed only once! After that the code will be executed until the second else if statement before the number will get 0 or 1. A smaller number of calculated boolean expression will result in a faster calculation.

isEven (better version)

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

countBs

    function countBs(myString) {
      var counter = 0;
      for (index = 0; index < myString.length; index++) {
        if (myString[index] == "B") {
          counter++;
        }
      }
      return counter;
    }
    console.log(countBs("ABC, BOT"));

countChar

    function countChar(myString, myChar) {
      var counter = 0;
      for (index = 0; index < myString.length; index++) {
        if (myString[index] == myChar) {
          counter++;
        }
      }
      return counter;
    }
    console.log(countChar("ABC, BOT, BEAN","B"));

countBs using countChar

If we already have developed countChar, we don’t need countBs any more, since countChar is more general than countBs. We could even write a shorter countBs using countChar:

    function countChar(myString, myChar) {
      var counter = 0;
      for (index = 0; index < myString.length; index++) {
        if (myString[index] == myChar) {
          counter++;
        }
      }
      return counter;
    }

    function countBs(myString) {
      return countChar(myString, "B");
    }
    
    console.log(countBs("ABC, BOT, BEAN"));
1 Like

This is in a league of it self. Nice job with everything. :+1:

Thanks for helping out.
Is it ok if I borrow the picture to future posts?
Ivo

Hello ivga80,

sure you can borrow the picture for future posts. We are a community and are supposed to help each other out.

Regards

Ouzo69

1 Like

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

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

Edit @ivga80 (Hi. You forgot your curly brackets { } :wink:)

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

else
return b;
}

2.var isEven= function(num){
num=Math.abs(num);
if(num===0)
return true;
else if(num===1)
return false;
else return isEven(num-2);

}

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

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

1 Like
  1. //Minimum

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

  1. //Recursion

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

  1. //Bean Counting

var count = 0;

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

var counter = 0;

function countChar(string, char){

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

if (string[i] == char){

counter++;

}
}
return counter;
}

1 Like