Chapter 3 Exercises

Minimum

function min(a,b) {
  return Math.min(a,b)
}

console.log (min(3,4))

Recursion

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

console.log (isEven(-7))

Bean Counting

function countBs(myString) {
  let myCount = 0
  for (let i = 0; i < myString.length; i++) {
    if (myString[i] == "B") {
      myCount ++}
    }
    return myCount
  }
console.log (countBs("aBcdB"))

function countChar(myString,myChar) {
  let myCount = 0
  for (let i = 0; i < myString.length; i++) {
    if (myString[i] == myChar) {
      myCount ++}
    }
    return myCount
  }
console.log (countChar("abacab", "a"))
1 Like

Minimum

    function min(num1, num2) {
    return num1 < num2 ? num1 : num2;
}

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

Recursion

    function isEven(num) {
    if (num < 0) return "Number must be a positive integer!";
    if (num == 0) return "Even";
    if (num == 1) return "Odd";
    return isEven(num - 2);
}

console.log(isEven(-1));
1 Like

Bean Counting

    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("Blue Jean Baby!"));
console.log(countChar("Beginning Shell Scripting.", 'i'));
1 Like

Min:
function min(x,y){return math.min (x + y);} var answer = (25, 18); alert(answer);
-18

**Recursive:**Yea, I just keep getting this wrong. I’m really not sure about these :frowning: here is what I’ve been putting in.
1.function isEven(x) {var x = “%2”; return isEven(x);}
2function isEven(x,y) {if(x== %2) return True;} else alert(false); {if(y == %2) alert(“you got it!”);}
I just played with different variations of these two and it just doesn’t come out right so I could use the help. I’m going to look at the answers for this tho.

Ok guys, I have to come back to these. :frowning:

Yea this is embarrassing. I didnt do well here at all.

Minimum

function min (num1 , num2) {
  if (num1 < num2) {
    return num1;
}
  else {
    return num2;
  }
}
console.log (min(1 , 2));
  // → 1

Recursion

function isEven(outcome) {
  if (outcome < 0) {
    outcome = -outcome
  }
  if (outcome == 0) {
    return true;
  }
  else if (outcome == 1) {
    return false;
  }
  else {
   return isEven(outcome -2)
  }
}
console.log(isEven(50));
  // → true
console.log(isEven(75));
  // → false
console.log(isEven(-1));
  // → false

Bean Counting

function countBs(input) {
  return countChar(input, 'B');
}
function countChar(input , character) {
  let result = 0;
  for (let i = 0; i < input.length; i++) {
    if (input[i] === character) {
      result = result + 1;
    }
  }
  return result;
}
console.log(countBs("BBQ"));
  // → 2
console.log(countChar("Character", "a"));
  // → 2
1 Like

/Minimum/
const minimum = (firstArgument, secondArgument)=>{
const lowerNumb = firstArgument < secondArgument ? firstArgument : secondArgument
return lowerNumb
}

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

/Bean Counting/

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

let numerOfChars = 0
const countChar = (string, char)=>{
for (let i = 0; i < string.length; i++){
if (string[i] == char) {
numerOfChars += 1;
}
}
return numerOfChars
}

1 Like
  1. Minimum
    var arr = [8, 9, 4, 1, 9, 23];

var min = arr[0];

for (var i = 0; i < arr.length; i++); {
if (arr[i]<min) {
min = arr[i];
console.log (“Setting min to” + min);

}
}
console.log(min);

  1. 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(-11));

1 Like

Minimum :
image
Recursion:
image
Bean count:

1 Like

Minimum
const Min = function (a, b){
if (a > b) {
return b;
}
if (a < b) {
return a;
}
}

Recursion
function isEven(number) {
if (number <= -1) {
return “number needs to be whole and positive”;
}
if (number === 0) {
return “even”;
}
if (number === 1) {
return “odd”;
}

return isEven(number-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;
}

1 Like

Hey @Malik is there anyway you can help me to understand these a little better. I really don’t want to miss this information. Still it is getting a bit mroe difficult for me. Thanks.

Minimum

    <script>

    function minimum (a, b){

      if (a < b ) return a;

      else return b;
    }

    alert(10, 20)
    alert(-1, 10)
    alert(100, -5)
    
    </script>

Recursion

    <script>

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


    </script>

Bean Counting

    <script>

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

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

    alert("The number of 'Bs' is " + countBs("BTC"));

    alert("The Number of Characters is " + countChar("bitttttcoin", "t"));

    </script>


2 Likes

You can check this post for reference,

Also,

This above statement is incorrect. You are assigning a string to the x variable which doesn’t make sense in this context. So, do not go ahead with this implementation.

The syntax here is incorrect. You can take reference from the above student answers for the correct answers. One of them is –

Hope this helps.

2 Likes
  1. Minimum

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

min(0, -10);

  1. Recursion - This one took me a bit longer - first attempt I didn’t actually use recursion.

// First attempt

function isEven(x) {
if (x % 2 == 0) {
return true;
} else {
return false;
}
}
console.log(isEven(50));
console.log(isEven(75));
console.log(isEven(-25));
console.log(isEven(-50));

// Second attempt

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

I couldn’t get the third one! Going to have to keep on studying until I understand it :slight_smile:

1 Like
    function min(a, b) {

        if (a<b) return a;
        else return b;
    }

    function isEven(num) {

        if (num % 2 == 0) return true;
        else return false;
    }

    function countBs(str)
    {
        var numBs = 0
        var len = str.length
        for (var count = 0; count < len; count++) {
            if(str[count] == "B") numBs++;
        }
        return numBs;
    }

    function countChar(str1, str2){
        var numChars = 0
        var len = str1.length
        for (var count = 0; count < len; count++) {
            if(str1[count] == str2) numChars++;
        }
        return numChars;
    }
2 Likes

1. Minimum

function min(a,b) {
if  (a===b) {console.log(a + " equals " + b);}
else if (a>b) {return b}
else {return a}
}
console.log(min(9,40));

9

2. Recursion

Couldn’t figure it out at all(( It’s a shame but had to finally check the right answers. I was also wondering if it is possible to create a program with the same idea, but using a loop, that runs N-2 until it gets either to 0 or 1… Did anyone try? All my attempts were unsuccessful.

3. Counting beans.
In the 1st program tried how it works with prompt method:

function countBs1() {
let word= prompt("Enter a string"); 
let match = (word.match(/B/g) || []).length; 
console.log(match);
} 
console.log(countBs1());
function countChar(word, char) { 
let charCount = 0; 
for (let location = 0; location < word.length; location++) {
if (word.charAt(location)==char) {
charCount+=1;
} 
} 
return charCount;
} 
console.log(countChar("hfhfghegfhgfgf","g"));

4

1 Like

I have been trying to solve the assignment on Bean counting and after several attempt and looking for solutions on google, I have a better understanding of the question but I still can’t get my code to work and worst still non of the codes here that I have tried seems to execute without an error message. I am terribly stuck, I need help
Thanks

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

Recursion

function isEven(a) {
if (a < 0) {
a = - a

} else if (a == 0) {
return true;

} else if (a == 1) {
return false;

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

Count Bs
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
function min(x,y){

    if (x < y) return x;

    else if (x == y) return console.log("numbers are equals");

    else return y 

} 



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

 }
1 Like