Chapter 3 Exercises

Chapter three exercises:

  1. Minimum
    function myMinimum(a,b){
    if (a<b)
    return a;
    else
    return b;
    }

  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 countsB(n)
    {
    return countChar(n,“B”);
    }

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

1. Minimum

<html>
  <body>
    <script>
    function min(a,b){
      if(a<b){
      document.write("a")
    }
      else{
      document.write("b")
      }
    }
    min(1809,1034)
    </script>
  </body>
</html>

2. Recursion

<html>
  <body>
    <script>
    var one = "odd"
    var zero = "even"
    function isEven(n){
      if(n>0){positive(n)
        function positive(n){
          var positiveReturn = n-2;
        if(positiveReturn == 0){
        document.write(zero);
        }
        else if(positiveReturn == 1){
        document.write(one)
        }
        else{
        positive(positiveReturn-2);
        }
      }
      }
      else if(n<0){negative(n)
        function negative(n){
          var negativeReturn = n+2;
          if(negativeReturn == 0){
          document.write(zero);
          }
          else if(negativeReturn == 1){
          document.write(one)
          }
          else{
          negative(negativeReturn+2);
            }
          }
      }
    }
    isEven(-1)
    </script>
  </body>
</html>

3a. Bean Counting

<html>
  <body>
    <script>
    function countBs(string){
      let numberOfB = 0;
      for(let n=0; n<string.length ; n++){
       if(string[n] == "B"){
         numberOfB = numberOfB+1;
       }else {
         numberOfB=numberOfB+0;
       }
       }
       document.write(numberOfB);
     }
    countBs("BigBoxOfBalloonsInABakery")
    </script>
  </body>
</html>

3b. Bean Counting

<html>
  <body>
    <script>
    function countChar(string, letter){
      let numberOfLetter = 0;
      for(let n=0; n<string.length ; n++){
       if(string[n] === letter){
         numberOfLetter = numberOfLetter+1;
       }else {
         numberOfLetter=numberOfLetter+0;
       }
       }
       document.write(numberOfLetter);
     }
    function countBs(string){
      return countChar(string, "a");
    }
    countBs("alabama bahama Mama")
    </script>
  </body>
</html>
1 Like

MINIMUM
function min(x,y) {
if (x<y){
console.log(x);
}else if (y<x){
console.log(y);
}else{
console.log(“x and y are the same”);
}
};
min(3,3)
RECURSIVE
function isEven(num) {

if (num < 0) num = -num; // remove negative sign

switch (num) {

case 0: return true;

case 1: return false;

default: return isEven(num-2);

}

}

BEAN COUNTING
this one worked for me:

function countBs(string){

let resultB=0;
let resultA=0;
for (let count=0; count<string.length; count++){
string[count]==“b” ? resultA ++ : resultA=resultA;
string[count]==“B” ? resultB ++ : resultB=resultB;
}
return [resultA, resultB];
}
let answer=countBs(“BeansBeansBbbBeans”);
alert(answer);

1 Like

Minimum

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

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

Recursion

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

console.log(isEven(50));
// → true
console.log(isEven(75));
// → false
console.log(isEven(-1));
// → ??

the console.log for -1 returns error message “maximum stack call exceeded”, because the function recalls itself until 0 or 1 is reached, but a number less than one has no command to return “true” or “false”; this can be fixed by adding this line: else if (a < 0) return isEven(-a); before this line: else return isEven (a - 2);

Bean counting

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

console.log(countBs(“BBC”));
// → 2
console.log(countChar(“kakkerlak”, “k”));
// → 4

1 Like

Exercise 1 - Minimum
var x = prompt(“Please enter first value”);
var y = prompt(“Please enter second value”);
function Min(x,y){
if (x < y) return x;
else return y;
}

var answer = Min(x,y);
alert (answer);

Exercise 2 - recursion
var x = prompt(“Please enter value”);

function isEven( x ){
if (x == 0) return true;
else if (x == 1) return false;
else if (x < 0) return isEven(-x); //solution for maximum call stack size exceeded
else return isEven(x - 2);
}

var answer = isEven(x)
alert(answer)

Exercise 3 - counting bean
function countBs(word){
let count = 0;
for (i=0;i<word.length;i++){
if (word[i]===“B”) count += 1;
}
return count
}
var answer = countBs(“BbubbBbleBuBm”)
console.log(answer)

function countChar(word,char){
let count = 0;
for (i=0;i<word.length;i++){
if (word[i]===char) count += 1;
}
return count
}
var answer = countChar(“BbubbBbleBuBmuuuuu”,“u”)
console.log(answer)

1 Like

Minimum:

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

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

Exercise 1 Minimum:

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

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

Exercise 3 Bean Counting:

function countBs(x) {
let token = 0;
for (i = 0; i < x.length; i++)
{
if (x.charAt(i)==‘B’)
token++;
else
token = token + 0;
}
return token;
}

function countChar(x,y) {
let token = 0;
for (i = 0; i < x.length; i++)
{
if (x.charAt(i)==y)
token++;
else
token = token + 0;
}
return token;
}

if(x[i] === "B"){
token++
}
return token;

They both work the same.
When I learned Java in school I remember we had to use charAt() to pick a character from a String, so looked it up to see if it’s the same in Javascript and got the syntax from W3. I didn’t know you could just do x[i], I guess that’s an easier way to do it.

1 Like

MINIMUM:

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

RECURSION

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

else if (x<0) return isEven(-x); solves the problem.

BEAN COUNTING

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

This is what i have so far and in console log it says even odd odd
did i do this right?

    <h1> Title </h1>


    <script>

        function isEven(a){
          if (a == 0){
            return ("Even");}
            else if (a == 1){
              return ("Odd");}
                else if (a < 0){
                  return isEven(-a);}
                  else{
                    return isEven(a-2);}}

                    console.log(isEven(50));
                    console.log(isEven(75));
                    console.log(isEven(-1));
                    
    </script>
function isEven(a){
// % symbol called the Modulo Operator. It returns the remainder of dividing the left hand operand by right hand operand.
if(a % 2 === 0){
  return ("Even");
}
else{
 return ("odd");
}
}
1 Like
great website
    <h1> Title </h1>


    <script>

        function isEven(a){
          if (a % 2 === 0){
            return ("Even");}
            else if (a == 1){
              return ("Odd");}
                else if (a < 0){
                  return isEven(-a);}
                  else{
                    return isEven(a-2);}}

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

    </script>

  </body>
1 Like

MINIMUM FUNCTION

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

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

//If we change from a-2 to a+2 in the above line the program won’t crash and will work even if the given number is negative.

console.log(isEven(50));
// → true
console.log(isEven(75));
// → false
console.log(isEven(-1));
// ??

COUNTING FUNCTION

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

console.log(countChar(“BBC”,“B”));
// → 2
console.log(countChar(“kakkerlak”, “k”));
// → 4

1 Like