Chapter 3 Exercises

Minimum
function min(x, y) {
return Math.min(x,y);
}

console.log (min(17,98))

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

Function 1

Function 2

Function 3

var countChar2 = function(str, character) {
var count = 0;
for (var i = 0; i < str.length; i++) {
if (str[i] === character)
count++;
}
return count;
}
console.log(countChar2(‘brbrbrbrb’, ‘b’));

Function 1

Minimum

const minCalc = function(a,b) {
  if (a < b) {return a;}
  else if (a > b) {return b;}
  else {return "Both are equal";}
}
console.log(minCalc(24214, 24529)); //-> 24214

Recursion

function isEven(N){
  function isEvenAbs(M) {
    if(M==1) {
      return M!=1;
    } else if(M==0) {
      return M==0;
    } else {
      return isEvenAbs(M-2);
    }
  }
  return isEvenAbs(Math.abs(N));
}
console.log(isEven(50)); //-> true
console.log(isEven(75)); //-> false
console.log(isEven(-1)); //-> false

Bean Counting

function countChar(string, char) {
  var counter = 0;
  for(i=0; i < string.length; i++) {
     if(string[i]==char){
       counter++;
     }
  }
  return counter
}
console.log(countChar("bBb BbAa AcCCAD dac", "A")); //-> 3
  1. Minimum Function:
    function minimum (a,b)
    {
    if (a<b){
    return a;
    }
    else if (a==b){
    document.write(“a and b are equal!”);
    }
    else {
    return b;
    }
    }
    var min = minimum (5,10);
    alert (min);

  2. Recursion Function:
    function isEven (N)
    {
    if (N >= 0)
    {
    while (N>1)
    {
    N -= 2;
    }
    if (N==0)
    {
    document.write (“N is even!”);
    }
    else
    {
    document.write (“N is odd!”);
    }
    }
    else
    {
    while ( N < -1)
    {
    N += 2;
    }
    if (N==-1)
    {
    document.write (“N is odd!”);
    }
    else
    {
    document.write (“N is even!”);
    }
    }
    }
    isEven (-88);

  3. Bean Counting Function:
    function countChar (string, character)
    {
    var count = 0;
    for (var i = 0; i<string.length; i++)
    {
    if(string.charAt(i) == character)
    {
    count = count + 1;
    }
    }
    return (count);
    }
    console.log (countChar (“Gu Yan is a BaBy.”, “Y”));

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

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

Counting Bs:
function countBs(name) {
var numberOfBs = 0;
for (var N = 0; N < name.length; N++) {
if (name[N] == “B”) {
numberOfBs ++;
}
}
return numberOfBs;
}

CountingChar:
function countChar(name, letter) {
var numberOf = 0;
for (var N = 0; N < name.length; N++) {
if (name[N] == letter) {
numberOf ++;
}
}
return numberOf;
}

function countBs(name) {
  return countChar(name, "B");
}
1 Like

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

function isEven(a){
if(a<1){
console.log(“This number is not a positive integer, error!”);
return -1;
}
else {
let n=a%2;
if(n==0) {return true};
return false;
}
}

function countBs(word){
let times=0;

for(let i=0;i<word.length;i++){
if(word[i]==‘B’){
times++;
}
}
return times;
}

function countSymbols(word, s){
let times=0;

for(let i=0;i<word.length;i++){
if(word[i]==s){
times++;
}
}
return times;
}

1 Minimum Code

        var x = prompt("Enter the first Number");
        var y = prompt("Enter the second Number");
        document.write(min(x , y))
        function min(a , b) {
          if(a<b)
          {
            return a;
          }
          else {
            return b;
          }
        };

2 Recursion Code

        var result = IsEven(-1);
        document.write(result);
        function IsEven(number) {
          if(number == 0)
          {
            return true;
          }
          else if(number == 1)
          {
            return false;
          }
          else if(number<0)
          {
            var NegNumber = number * (-1);
            return IsEven(NegNumber);
          }
          else {
            return IsEven(number - 2);
          }
        }

3 Bean Counting

        var GivenStr = prompt("Give a string in UPPER_CASE bro");
        var SecondString = prompt("Enter the string bro");
        var CharacterToMatch = prompt("Enter the character which you want to match in the second given string");
        CountBs(GivenStr);
        CountChar(SecondString , CharacterToMatch);
        function CountBs(ExceptedStr){
          var k = 0;
          for (var i = 0 ; i<= ExceptedStr.length - 1 ; i++)
          {
            if(ExceptedStr[i] == "B")
            {
              k++;
            }
          }
          document.write("The counting number of \"B\" in UPPER_CASE string is " + k);
        }
        function CountChar(Rajesh , Shreya){
          var k = 0;
          for (var i = 0 ; i<= Rajesh.length - 1 ; i++)
          {
            if(Rajesh[i] == Shreya)
            {
              k++;
            }
          }
          document.write("The number of character you given in second string is " + k);
        }

i did it like this

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

console.log(isEven(50));
and the others

it worked. i ve seen other programs that look nothing like this. is this actually ok?

damn i just literally asked the same question. i did it the same way you did

//MIN FUNCTION
let min = function(a,b) {
if (a > b){
return b
} else {
return a
}
}

//IS EVEN RECURSIVE
function isEven(num) {
if (num < 0 ) {num = Math.abs(num)}
if (num > 1) {return isEven(num - 2)}
else if (num === 0) {return true}
else {return false}
}

//====BEAN COUNT======
function countChars(word, char) {
let cnt = 0
for (let i=0; i < word.length; i++) {
if (word[i] === char) {
cnt++
}
}
return cnt
}

//BEAN COUNT PART 2
function countBs(word) {
return countChars(word, ‘B’)
}

Hi Denci,

I reckon the answer is no - not because it doesnt work, but because the exercise is really all about recursive functions, ie getting a function to call itself. Recursive functions provide some useful ways of programming (apparently…) so that’s what we are trying to get used to. HTH, Cheers, Mark.

Finding a Minimum

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

Almost the same as the EloquentJavaScript code, but then again I felt it was a relatively simple code.

isEven Function

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

Upon checking the answer, it says I should have put a “return” function, under the line “isEven(number - 2);” can anyone tell me why? Should the program have just ran the function still?

Bean Counting

My code

function countBs(input){
let counter = 0
for (N = 0; N <= input.length - 1; N++){
if (input[N] == “B”){
counter ++;
}
}
return counter;
}

function countChar(input, letter){
counter = 0;
for (N = 0; N <= input.length - 1; N++){
if (input[N] == ${letter}){
counter ++;
}
}
return counter;
}

Now I realised that I could have put the countBs function inside countChar. And there were a few other minor mistakes. For example, in the ‘for’ function, I could have simply put the second condition as “N < input.length” instead.

1. Minimum

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

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

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(50));
console.log(isEven(75));
console.log(isEven(-1));

3. Bean Counting

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, charToCount) {
    let count = 0;
    for (let i = 0; i < string.length; i++) {
        if (string[i] == charToCount) {
            count += 1;
        }
    }
    return count;
}

console.log(countBs("BBC"));
console.log(countChar("kakkerlak", "k"));

My solutions for the Chapter 3 exercises are as follows:

        <script>
            function minOfTwo(a, b) {
                if (a <= b){
                    return a;
                } else {
                    return b;
                }
            }

            console.log("Minimum of 5 & 15 : " + minOfTwo(5, 15));

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

            console.log("6 is even? " + isEven(6));
            console.log("5 is even? " + isEven(5));
            console.log("-6 is even? " + isEven(-6));
            console.log("-5 is even? " + isEven(-5));


            function countChar(data, character = "B") {
                let count = 0;
                for(let i = 0; i < data.length; i++){
                    if(data[i] === character){
                        count++;
                    }
                }
                return count;
            }

            console.log("There are how many c's in connection? Answer: " + countChar("connection", "c"));
            console.log("There are how many b's in connection? Answer: " + countChar("connection", "b"));
        </script>

Get min Function

<script>
  const getMin = (a, b) => {
                if (a > b) {
                    result = b;
                } else if (a < b) {
                    result = a;
                } else {
                    result = "The two numbers are the same";
                }
                console.log(result);
            }
            getMin(5, 5);
</script>

Recursion

<script>
  const isEven = (n) => {
                if (n < 0) {
                    console.log("can't use numbers below 0");
                } else if (n % 1 != 0) {
                    console.log("the number can't have decimals")
                }
                let even_or_odd = n % 2;

                if (even_or_odd === 0) {
                    console.log("this is an EVEN number");
                }
                if (even_or_odd === 1) {
                    console.log("this is an ODD number");
                }
            }
            isEven(-1);
</script>

Bean Counting

<script>
function countBs(sentence, findChar) {
                let counter = 0;
                let charCount = countChar(sentence);
                for (i = 0; i < countChar(sentence, findChar); i++) {
                    if (sentence[i] == findChar) {
                        counter++;
                    }
                }
                let plurial = "";
                if (counter > 1) {
                    plurial = "s";
                }
                console.log("there are " + counter + " `" + findChar + "`" + plurial + " in this sentence !")
            }

function countChar(sentence) {
                return sentence.length;
            }
            countBs("I hate the Bold Bear Hunter", "B");
</script>
  1. function minimum(x,y){
    if (x < y) return x;
    else return y;

     }
     console.log(minimum(9,5));
    
  2. var odd = 1;
    var even = 0;
    function isEven(N)

    {

         if((N - 2) % 2 == even)
         {
           return true
         }
         else
         {
           return false
         }
    
       }
       console.log(isEven(50));
    
  3. 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”);
    }

a function
min that takes two arguments and returns their minimum

<script>
var result = 0;
function myMin(x,y) {

                          if (x < y) {
                              result = x ;
                          } else if (y < x) {
                              result = y ;
                          } else {
                              result = "they are the same";
                          }

                     }

myMin(6,8);

document.write(result);
console.log(result);

isEven

function isEven(num){

  if(num<=0){
      num *= -1;
    }
    // coverts negative numbers to positive

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

console.log(isEven(-80));

Beancounter

function countChar(bs, letter){

var res = bs.split("");

var numOfhits = 0;
 for(var i=0;i<res.length;i++){
    if(res[i] === letter)
       numOfhits++;
                                  }

document.write(res);
return numOfhits;

}



console.log(countChar("BBgBBBBiiiBBB", "i"));