Chapter 3 Exercises

If have a question on the recursive functions.IN one of the examples the is. a line

return find(current + 5, (${history} + 5)) ||

What does the `(${history} do ? it really makes it hard when they describe a function but we have not learned what certain Javascript parameters actually do and how they work.

1 Like
    // Minimum

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

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

    // Recursion

    function isEven(a){
      if(a < 0){
        return "The number must be greater than zero";
      }
      if(a == 0){
        return true;
      }
      if(a == 1){
        return false;
      }
      else{
          return isEven(a - 2);
      }
    }

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

    // Bean Counting

    function countBs(string, lookingFor = "B") {
        let found = 0;
        for (let n = 0; n < string.length ; n++){
            if(string[n] == lookingFor) {
                found++;
            }
        }
        return found;
    }

    console.log(countBs("BBCSBfaB"));
1 Like

1. Excercise 1 - Minimum

function min (x,y){
if (x < y){
alert(x)
}
else if (x > y){
alert(y)
}
}
min (11, 10);

Excersice 2 - Recursion

function isEven(a){
if(a < 0){
alert (“Please no Negative numbers, thanks!”);
}
else if (a == 0) {
alert (“true”);
}

else if (a == 1){
alert (“false”);
}

else{
return isEven(a - 2);
}
}

isEven(8)

Excersice 3 - Beans

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 countBs(string){
let count=0;
for(let i=0; i<string.length; i++){
if(string[i]==“B”){
count+=1;
}
}
}
function countChar(string, char){
let count=0;
for(let i=0; i<string.length; i++){
if(string[i]== char){
count +=1;
}
}
return counted;
}
function countBs(string){
return countchar(string, “B”);
}

1 Like

Hi, thanks so much for your assistance with the dynamic fruitlist assignment but after making the correction it still won’t run on my computer meanwhile it ran successfully on yours. I have actually been stuck ever since and tried to unstuck myself to no avail. Please I still need help!!! Thanks

Please share the updated code as well.

Thanks.
A.Malik

1. Minimum

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

printMin (12, 33));
//→ returns 12

2. Recursion

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

isEven(50);
//→ returns true

3. Bean Counting

function countBs (string) {
let result = 0;
 for ( let i = 0; i <= string.length; i += 1) {
  if (string.charAt(i) === 'B')
     result += 1;
}
return result;
}

countBs("BigBootyB....es");
// → returns 3
1 Like
  1. Minimum Exercise
            function minNumber(arg1, arg2) {
                document.write(Math.min(arg1, arg2));
            }
            minNumber(400, 300);

Alternative Without Using Math.min

            function minimumNumber(args1, args2) {
                if (args1>args2) {
                    return (args2);
                } else if (args1<args2) {
                    return args1;
                } else {
                    return "These are Equal!";
                }
            }
            document.write(minimumNumber(1, 3));
  1. Recursion Exercise
             function isEven(num) {
                if (num == 0) {
                    return "Number is Even";
                } else if (num == 1) {
                    return "Number is Odd";
                } else {
                    return isEven(num - 2);
                }
            }
            document.write(isEven(50));
            document.write(isEven(75));

Fix for a Negative Number

function isEven(num) {
// These two lines below have been included to firstly negate a negative number
                if (num < 0) {
                    return isEven(num * -1);
// ... And then continue as per before:
                } else if (num == 0) {
                    return "Number is Even";
                } else if (num == 1) {
                    return "Number is Odd";
                } else {
                    return isEven(num - 2);
                }
            }
            document.write(isEven(-1));
  1. Bean Counting Exercise
function countBS(string) {
                var letterB = 0;
                for (var j = 0; j < string.length; j++) {
                    if (string[j] == "B") {
                        letterB = letterB + 1;
                    }
                }
                return letterB;
            }
            document.write(countBS("Babies")); // Prints "1"
            document.write(countBS("BaBies")); // Prints "2"
            document.write(countBS("Hello")); // Prints "0"

countChar Alternative

function countChar(newString, character) {
                var letterNum = 0;
                for (var k = 0; k < newString.length; k++) {
                    if (newString[k] == character) {
                        letterNum = letterNum + 1;
                    }
                }
                return letterNum;
            }
            document.write(countChar("Plebs", "P")); // Prints "1"
            document.write(countChar("plebs", "P")); // Prints "0"
            document.write(countChar("PerPhekt", "P")); // Prints "2"
1 Like

Hi, Could someone please help me to understand this: (pg 48)

If you write an = operator after a parameter, followed by an expression, the
value of that expression will replace the argument when it is not given.
For example, this version of power makes its second argument optional. If
you don’t provide it or pass the value undefined, it will default to two, and the
function will behave like square.

function power(base, exponent = 2) {
let result = 1;
for (let count = 0; count < exponent; count++) {
result *= base;
}
return result;
}
console.log(power(4));
// → 16
console.log(power(2, 6));
// → 64

I understood the 1st argument = 16
But not the 2nd one. Why is it 64?

And this 1 (pg 50)

function power(base, exponent) {
if (exponent == 0) {
return 1;
} else {
return base * power(base, exponent - 1);
}
}
console.log(power(2, 3));
// → 8

How it’s = 8?

Thanks,

1 Like

// IsEven

function IsEven(n) {

  if (n < 0) n = -n;

  if (n == 0) return 1;
  else if (n == 1) return 0;
  else return IsEven(n-2);

}

// counting character functions...


function countBs(s) {

  count = 0;
  for (var i = 0; i < s.length; i++) count += (s[i] == 'B');

  return count;
}

function countchar(s,c) {

  count = 0;
  for (var i = 0; i < s.length; i++) count += (s[i] == c);
  return count;
}
1 Like

What are we supposed to do for the minimum excercise??

Recursion exercise:

function isEven(number){
if (number % 2 == 0){
return(“true”)
}
else{
return(“false”)
}
}
console.log(isEven(50))
console.log(isEven(75))
console.log(isEven(-1))

:rofl: :rofl: :rofl: :rofl: :rofl: :rofl: .

1 Like

Minimum

function mn(a, b) {
               return a>b ? b : a;
           }

Recursion

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

Bean Counting

function countChar(word, chr){
              if(chr.length>1){
                  return 0;
              }
              let counter =0;
              for(let i=0;i<word.length;i++){
                  if(word[i]==chr){
                      counter++
                  }
              }
              return counter;
          }
1 Like

Min:

let a = window.prompt(“Ïnsira o primeiro numero”);
let b = window.prompt(“Insira o segundo numero”);

function min (a, b)

{
if (a>b);
console.log(b)
}

if (a<b);

{
console.log(a)
}

function isEven(n)
{
if (n %2 == 0)
{
return true;
}

if (n %2 == 1)

{
 return false;
}

if (n < 0)
{
    return isEven(-n);
}

else
{
    return isEven(n - 2);
}

}
console.log(isEven(75));
// false
console.log(isEven(50));
// true
console.log(isEven(-1)):
// false

function countBs(myString)
{
return countChar(myString, ‘B’);
}

function countChar(myString, character) 

{
    let count = 0;
    while (myString.length > 0) 
    {
        if (myString.charAt(0) == character) count++;
        myString = myString.substr(1);
    }
    return count;
}

console.log(countBs("Baby blues, Bubba gun, boom Boom!"));
1 Like

Minimum:

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

Recursion:

        //var even = "The number is even";
        //var odd = "The number is odd";
        var evenNum = 0;
        var oddNum = 1;

        function isEven(number) {
          if(number == evenNum) {
            return true;
            //console.log(even);
          }else if(number == oddNum) {
            return false;
            //console.log(odd);
          }else if(number < 0){
            return isEven(-number);
          }else{
            number -= 2;
            return isEven(number);
          }
        }
        console.log(isEven(50));
        console.log(isEven(75));
        console.log(isEven(-1));

Bean Counting:

        function countBs(anyWord){
          return countChar(anyWord, "B");
        }
        console.log(countBs("BBC"));

        function countChar(anyWord, letter) {
          var letterAmount = 0;
          for (var position = 0; position < anyWord.length; position++){
            if(anyWord[position] == letter) {
              letterAmount++;
            }
          }
          return letterAmount;
        }
        console.log(countChar("kakkerlak", "k"));
1 Like

// Minimum

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

// Bean counting

Part 1:

function countBs(string) {
let result = 0;

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

Part 2:

function countChar(string, character) {
let result = 0;

for (let i = 0; i <= string.length; i++) {
if (string[i] === character) {
result = result + 1;
}
}
return result;
}

Part 3:

function countBs(string) {
return countChar(string, ‘B’);
}

function countChar(string, character) {
let result = 0;

for (let i = 0; i <= string.length; i++) {
if (string[i] === character) {
result = result + 1;
}
}
return result;
}

1 Like
  1. Minimum

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

  2. Recursion

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

  1. BeanCounting

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

 function countChar(char, string){
    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;
}
}

min(7 ,1)

RECURSION

// Define a recursive function isEven
function isEven(n) {

// what if n is negative. I did not really get this part. How does this solve the problem??
if (n < 0) {
n = -n
}

// Zero is even. (Return boolean)
if (n === 0) {
return true;
}

// One is odd. (Return boolean)
if (n === 1) {
return false;
}
// If n is not (0 || 1), subtract 2 from n until it is equal to 0 or 1
return isEven(n - 2);
};

console.log(isEven(6))
// true
console.log(isEven(5))
// false

// if n == negative there is no way of subtracting to reach 0 || 1

// BEAN COUNTING

// function has a (string) as its only argument

function countBs(input) {
// create a result, set to 0 (will be returned at the end)
let result = 0;

//loop over the string
for (let i = 0; i < input.length; i++) {
  //if current character is a "B"
  if (input[i] === "B") {
    // increment our result by 1
    result++;
  }
} 
  // return result
  return result;

};
//return result
return result;
}

function countChar(string, character) {
let result = 0;

//loop over the string
for (let i = 0; i < string.length; i++) {
  //if current character is a "B"
  if (string[i] === character) {
    // increment our result by 1
    result++;
  }
} 
  // return result
  return result;

};

1 Like

I received the instructions (thanks Malik) and I post them here in case it will help someone:
console.log(power(4));
Here, the base is 4 and the exponent is 2 (by default since nothing passed). Therefore, the result would be 4 * 4 which is equal to 16.
console.log(power(2, 6));
Here, the base is 2 and the exponent is 6 (by passing an explicit value and disregarding default). Therefore, the result would be 2 *2 *2 *2 *2 *2 which is equal to 64.

The second question -
console.log(power(2, 3));
Here, we are using recursions instead of a normal for-loop to multiply the numbers. So, for every recursion, the base keeps getting multiplied. Therefore, the answer would look like this –
Step 1 - 2 * power(2,2)
Step 2- 2 * 2 * power(2, 1)
Step 3 - 2 * 2 * 2 * power (2, 0)
step 4 - 2 * 2 * 2 * 1 [which equals 8]

1 Like