Chapter 3 Exercises


#62

1. Minimun function

// min funcion
      function min(num1, num2) {
          if (num1 > num2)
            return num2;
          else
            return num1;
      };

      console.log(min(7, 16));

2. isEven function
I have avoided multiplication by -1 by doing the addition for negative numbers

// isEven with negative support
      function isEven(number) {
          if (number == 0)
            return console.log(true);
          else if (number == 1)
            return console.log(false);
          else if (number < 0)
            isEven(number + 2);
          else
            isEven(number - 2);
      };

      isEven(50);

3. Bean counting function

// countBs funcion V1.0
      function countBs(string) {
        let count = 0;
          for(let pos=0; pos < string.length; pos++){
            if(string[pos] == "B")
              count++
          }
        return count;
      };

      console.log(countBs("abBa"));
      console.log(countBs("BabBaB"));

Here is the function for any char
function countChar(string, char) {
let count = 0;
for(let pos=0; pos < string.length; pos++){
if(string[pos] == char)
count++
}
return count;
};
// countBs funcion V1.0
function countBs(string) {
return countChar(string, “B”);
};


#63

Minimum

function min(x,y)
{
   if(x <= y)
      return x;
   else
      return y; 
};
console.log(min(6,2));

Recursion

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

Bean Counting

function countBs(str)
{
    //let cont = 0; 
    //for(let i = 0; i < str.length-1; i++){
    //    if (str[i] == "B")
    //        cont++;
    //}
    //return cont;
   
    return countChar(str,"B");
}

function countChar(str, char)
{
    let cont = 0;
    for(let i = 0; i < str.length-1; i++){
        if (str[i] == char)
            cont++;
    }
    return cont;
 }
 console.log(countBs("BooBaBoom"));

#64

Exercise 1: Minimum

function min(x, y) {
if (x < y ) return x;
else return y;
}
min(6, 11);
// --> 6

Excercise 2: Recursion

I have two answers here (A & B). ‘A’ is the answer I first put together. After looking at the solutions and other’s answers, I realised that my answer was not what the question was asking for. So answer ‘B’ is the correct answer.

A

function isEven(N) {
var zero = “true”
var one = “false”
if ((N-2) % 2 == 0)
return zero;
else return one;
}
console.log(isEven(-1));
// --> false

B

I learnt in question B from @Guactoshi , that if a number larger than 1 is used in the function, the code will call n-2 to ‘iterate’ itself until the number becomes a 1 or 0.

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(-1));
// --> false

Exercise 3: Bean Counting

A

I learnt here that the formula given, “string”[N], can used in an if statement, to solve for a number to become equal to a letter.

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

B

Didn’t quite understand this exercise. However, looking at the solution (below), I can determine somewhat how a function worked to count a yet undefined character from a string.

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

console.log(countBs(“Belle the Baby”));

console.log(countChar(“Coding for cats”, “d”));


#65

Hey satoshispockamoto.
I don’t know if you already received an answer to your question but here is the problem with your counter function.
you parse through the string received as a parameter on character at a time and compare it with a variable called char. This variable is not defined. So either you pass this variable as a parameter to the function or, if you want to count the "B"s than replace it with “B”.

I hope this help. If not just write me for any help.

See ya.
Mihai


#66

Hey wideyedwonderer.

Within the isEven function you call recursively again your function but do not use the return value of it.
You need to add return isEven(x). or even return isEven(x-2) and drop the line above also.
In this way your function will return true if x is 0, false if x is 1 and return whatever comes back from the call of isEven with x decreased by 2.
So any number (positive) you pass through your function will be passed again to the function decreased by 2 until it hits 0 or 1. And then it will chain return this value back through the recursive call.

I hope this helped. If not please pm me for additional explanations.

See ya,
Mihai


#67

Minimum

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

Recursion

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

Bean Counting

  function countBs(ourstring){
           var count = 0;
           for(var i = 0; i < ourstring.length; i++){
               if(ourstring[i] == 'B'){
                 count++;
               }
           };
     console.log(count);
     };
      function countChar(ourstring, chartofind){
                var count = 0;
                for(var i = 0; i < ourstring.length; i++){
                    if(ourstring[i] == chartofind){
                      count++;
                    }
                };
          console.log(count);
          };

#68
  1. function min(a,b){
    return Math.min(a,b)
    };

document.write("smallest number is " + min(55,978));

2.function isEven(k)
{
if (k == 0) return “

even!”;
else if (k == 1) return “

odd!”;
else return isEven(k - 2);
}
document.write("

your number is " + isEven(79));


#69

what if the numbers are the same or if someone enters characters other than numbers?


#70
  function min(first, second) {
    if(first < second) {
      return first;
    } else if(second < first) {
      return second;
    } else if(first == second) {
      return "first equals second";
    } else {
      return "what the heck happened?"
    }
  }

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

console.log(" ");
console.log("xxxxxxxxxxxxxxxxxxxxxxx")
console.log(" ");

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

console.log(isEven(-99));

console.log(" ");
console.log("xxxxxxxxxxxxxxxxxxxxxxx")
console.log(" ");

function countLetters(string, Letter){
  var count = 0;
    for(let i = 0; i < string.length; i++) {
      if(string[i] == Letter) {
        count = count+1;
      }
    }
    return count;
}

console.log(countLetters(" bbhjffdlsBafsjb", "b"));

#71

min could have been done with a switch statement. isEven could, of course, been done using a %. countLetters can use almost any string including spaces.


#72

so cool, to see that many different solutions. inspiring to learn different coding paths. thx to all.

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

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

  function countBs(getString) {
    var countString = 0;
    for (let counter = 0; getString[counter] != undefined; counter++) {
      if (getString[counter] == "B") {
        countString++;
      }
    }
    return countString;
  }


  function countChar(getString, testChar) {
    var countString = 0;
    for (let counter = 0; getString[counter] != undefined; counter++) {
      if (getString[counter] == testChar) {
        countString++;
      }
    }
    return countString;
  }

  console.log(min(7,-2));

  console.log(isEven(50));
  console.log(isEven(75));
  console.log(isEven(9));
  console.log(isEven(4));
  console.log(isEven(-5));
  console.log(isEven(-12));

  console.log("There are " + countChar(wordOne, printChar) + " " + printChar + "'s in string " + wordOne );
  console.log("There are " + countChar(wordTwo, printChar) + " " + printChar + "'s in string " + wordTwo );

#73

Minimum
function Minimum(x,y){
if(x<y){
return x;}
else return y;}
document.write(Minimum(32,14));

Odd/Even
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);
}
document.write(isEven(1));

Bean
function countChar(string, target) {
var counted = 0;
for (i = 0; i < string.length; i++) {
if (string[i] == target) {
counted += 1;
}
}
return counted;
}
document.write (countChar(“BBBean”, “B”))


#74

Minimum

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

Recursion

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

Bean counting

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


#75

that was very helpful thanks bird <3


#76

Minimum

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

Recursion

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

Bean Counting

function countChar(textstring, whichchar){
totalcount = 0

  for(i=0; i<textstring.length; i++){
       if(textstring[i] == whichchar){totalcount+=1;}
   }
   return totalcount;
}

#77

Hi guys. Here is my first exercise. (It is pretty basic.)

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

alert (Minimum(7.65, 7.6));

#78

With exercise two I did two versions, one before I read at the end that it needed to return a boolean. The first one seemed more polite, so I have included it also. :slight_smile:

function OddOrEven(n){
      if (n < 0) { n = 0 - n; }
      while (n > 2) { n -= 2 ; }
      if (n == 1) {
        return "odd";
      }
      else if (n == 2) {
        return "even";
      }
      else {
        return "not an integer";
      }
    }

    console.log("n is " + OddOrEven(-76)); 

Here it is after I reduced it to just return a boolean, and changed the function’s name.

    function isEven(n){
      if (n < 0) { n = 0 - n; }
      while (n > 2) { n -= 2 ; }
      if (n == 1) {
        return false;
      }
      else if (n == 2) {
        return true;
      }
      else if (n == 0) {
        return true;
      }
      else {
        return "not an integer";
      }
    }

    console.log("isEven? " + isEven(-72));
    alert(isEven(-72));


#79

I haven’t done my bean count yet, because I was firstly testing the method that the author suggested

"string"[N]

(to get the Nth character of a string)
which I haven’t figured out yet, and a cursory glance at others’ answers, it doesn’t look like anyone else is using that clue.

So, I will come back to this. The first two were not too hard, and they have already been posted above.


#80

Minimum:

function min(number1, number2) {
var min;
if (number1<number2) {
min=number1}
else min = number2;
return min;
}

Recursion:

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

Bean counting:

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

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


#81

Hey Guactoshi, I might be wrong I believe you are missing the return value for the countBs function, and also in countChar function the return value should be “count” instead of “counted”.

Please let me know if I am wrong. Thanks