Chapter 3 Exercises

Yes,
You can define it as a variable and then use it –

var myWord = "Bean";
vat myLetter = "B";
countChar(myWord, myLetter)
1 Like
  1. Minimum: This wasn’t too bad tbh…
function mathMin(a, b){
        if(a<b){
          console.log(a + " is smaller!");
        }
        else {
          console.log(b + " is smaller!");
        }
      }
      console.log(mathMin(4, 7));
  1. Recursion: Took me a while to get my head around this exercise… to solve the error that came about when inputting negative numbers, I just applied the same logic as (n-2) and made another if statememt using (n+2)… My thought process was that n has to eventually equal 0 or 1 in order for the boolean to be triggered, so to make a negative number into a positive, you have to use addition!
function isEven(n){
  if(n == 0){
    return ("This is an even number because it is " + true + "!");
  }
  else if (n == 1){
    return ("This is an odd number because it is " + false + "!");
  }
  else if (n>1){
    return isEven(n-2);
  }
  else if (n<0) {
    return isEven(n+2)
  }
}

console.log(isEven(-5));
  1. Bean counting (idk why it’s called bean :laughing:)… with this one, I have no idea if I have done this correctly or not… Please could someone have a look at the code and see if it’s working correctly? Many thanks!
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, char) {
    let count = 0;
    for (let i = 0; i < string.length; i++) {
      if (string[i] == char) {
     count ++;
   }
 }
return count;
}

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

I tested your code and it works perfectly! Good job and keep up the same great work!

Happy Learning! :muscle:

1 Like
//Return Minimum Function

  function min(a,b){
    if (a < b) return a;
    else return b;
  }
      console.log(min(21,2));
      console.log(min(5,7));


  // Recursion - isEven function
  // 0 is even
  // 1 is Odd
  // for number N, it's evenness is same as N-2

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

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

// Bean Counting

function countsBs(string){
  let count = 0;
  for (var i = 0; i < string.length; i++) {
    if (string[i] == "B") {
      count += 1;
      }
    }
    return count;
}
console.log(countsBs("BBnnmGHB"));

// Counting specifc character

function countsChar(string, char){
  let count = 0;
  for (var i = 0; i < string.length; i++) {
    if (string[i] == char) {
      count += 1;
      }
    }
    return count;
}
console.log(countsChar("BBnnmBGHB", "G"));
1 Like

Bean Counting
function count Bs(word){
var count = 0;
for(let i = 0; i <word.length; i++){
if(word[i] == "B"0{
count++
}
}
return count;
}
console.log(countBs(“BumbleBee”))

function countChar(word,myChar){
  var count = 0;
  for(let i = 0; i ,word.length; i++){
    if(word[i] == myChar){
      count++
    }
  }
  return count;
}
console.log(countChar("BumbleBee","e"))
1 Like

//Bean Counting ran well in Atom. However, when I run this in the Sandbox it does not take. Says //"unexpected token: identifier. I just noticed an error, line 5. Lets see if correcting lets it run?

Bean Counting
function count Bs(word){
var count = 0;
for(let i = 0; i <word.length; i++){
if(word[i] == “B”){
count++
}
}
return count;
}
console.log(countBs(“BumbleBee”))

function countChar(word,myChar){
  var count = 0;
  for(let i = 0; i <word.length; i++){
    if(word[i] == myChar){
      count++
    }
  }
  return count;
}
console.log(countChar("BumbleBee","e"))

// 2
//3

1 Like

function min(a, b) {
if (a > b) {
return b;
} else {
return a;
};
};
var result = min(2, 99);
alert(result);
2.
function isEven(x) {
if ((x - 2) % 2 == 0) {
return true;
} else {
return false;
}
};
var number = prompt(“What is the number?”);
alert(isEven(number));

Part 1
function countBs(string) {
let result = 0;
for (i = 0; i < string.length; i++) {
if (string[i] === “B”) {
result++;
}
}
return result;
}
Part 2
function countchar(string, character) {
let result = 0;
for (i = 0; i < string.length; i++) {
if (string[i] === character) {
result++;
}
}
return result;
}

1 Like

i d k what the necessity is of posting here since i have problems, but i’m not super into the programming side of things… i understand programming basically, but anyway here’s my homework after some guess and checks on the eloquent answer page…

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

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


function isEven(n) {
if (n<0) {return (isEven(-n));}
else if (n==0) {
return (“Even”);}
else if (n==1) {
return (“Odd”);}
else {return isEven(n-2);}
}
console.log(isEven(50));
console.log(isEven(75));
console.log(isEven(-1));


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

function countChar(string, x) {
let counter = 0;
for (let a=0; a < string.length; a++) {
if (string[a] == x) {
counter++;
}
}
return counter;
}

1 Like

Minimum

function min (x,y){
	 if (x<y){
	 	alert(x)
}
      else{ alert(y);}
	}
	min(2,1)

Recursion

 var number = prompt("Number please:");
  var input = isEven(number);
  document.write(input);
  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);
    }
  }

``
Bean Counting

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

function countBs(str)
{
    return countChar(str, 'B');
}
1 Like

Minimum

function min(a,b){

if(a>b) a=b;

return a;

}

1 Like

Recursion
function isEven(num){

var ret=false;

if(num<0){

console.log(“Please enter a positive number”);

return;

}

else if(num==0)

ret=true;

else if(num==1)

ret=false;

else

ret=isEven(num-2);

1 Like

Bean Counting

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

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

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

Exercise 2
function isEven(number) {
if (number % 2 == 0) {
console.log (“even”);
}
else if (number % 2 == 1) {
console.log (“uneven”);
}
else if (number < 0) {
if ((number * number) % 2 == 0) console.log (“even”);
if ((number * number) % 2 == 1) console.log (“uneven”);
}
}

I don´t have my own soultion for exercsie 3 as Id didn´t really get how to start with the soultion. But after looking at the solutions posted here I kind of get it. If you have any feedback please let me know.

1 Like
  // Chapter 3 Exercises
  // Minimum
  function minNumber(a, b) {
    let x = 0;
    if (a > b) {
      x = b;
    } else {
      if (b > a) {
        x = a;
      } else {
        // they are equal, a or b
        x = b;
      }
    }
    return x;
  }
  console.log(minNumber(15, 5));

  // Recursion
  function isEven(number) {
    // console.log(number); // for debugging

    // Check if the number is a negative number, otherwise
    if (number < 0) {
      return;
    }

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


  // Bean Counting
  function countBs(inputString) {
    return countChar(inputString, "B");
  }
  console.log(countBs("AnAtomyBBB"));

  function countChar(inputString, inputChar) {
    console.log(inputString); // for debugging
    let countResult = 0;
    for(let x = 0; x < inputString.length; x++) {
      if (inputString[x] == inputChar) {
        countResult++;
      }
    }
    return countResult;
  }
  console.log(countChar("AnAtomyBBB", "A"));
1 Like

Task1>

function Mymin(a,b){

                return a<b ? a:b;

            }

Task2

            function IsItEven(a){

                if(a<0) {a=-a;}

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

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

                else {return IsItEven(a-2);}

            }

Task3

            function countChar(word,C) {

                let counter=0;

                for (let i=0;i<word.length;i++){

                    if(word[i]==C){counter++;}

                }

                return counter;

            }

            function countBs(word){

                return countChar(word,'B');

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


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

      function countChar(string,C){
        var count = 0;
        for(var i=0;i<string.length;i++){
          if(string[i] == C){
            count++;
          }
        }
        return count;
      }
1 Like

1.function Minimum(x,y){ return Math.min(x,y) }; console.log(Minimum(8,1));

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

NOTE: When i try to put a -1 or any negative number for my isEven value the console output Maximum call stack size exceeded. Since neither of my if and else if statement are trigger to tell the program to return a value the computer continue to stack until it can’t no more. So i just put a else if (x < 0) return isEven(-x); to resolve the problem.

3.Bean counting:
3.1
var counted=0;
function countBs(string){
for( i=0;i<string.length;i++){
if(string[i]==“B”){
counted+=1;}
}
return counted;
}console.log(countBs(“BBQ”));
3.2
var counted=0;
function countChar(string,chr){
for( i=0;i<string.length;i++){
if(string[i]==chr){
counted+=1;}
}
return counted;
} function countBs(string)
{return countChar(string,“Q”);}
console.log(countBs(“BBQQQQQ”));

1 Like

// Minimum

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

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

//Recursion

function isEven(n) {

// a negative input for n would crash the program, this is how I prevented it:

if (n < 0) {
n = -n;
}

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

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

// Bean Counting

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

console.log(countBs("Babylon Battle"));
console.log(countChar("gogogo gadget", "g"));
1 Like

//Function Minimum
function minimum(a, b){
if(a <= b){
return a;
} else {
return b;
}
}
//console.log(minimum(2, 3))
//2

//Function Recursion
function isEven(N) {
if (N < 0) {
return isEven(N * -1);
}
else if (N == 0) {
return “This is an even number”;
}
else if (N == 1) {
return “This is an odd number”;
}
else {
return isEven(N - 2);
}
}

//Bean Counting Function Letter B
const countBs = (string) => {
let result = 0;
for (let i = 0; i < string.length; i++) {
if(string[i] == “B”){
result++;
}
}
return result;
};
console.log(countBs(“ABBA”))
//2
//Bean Counting Function Choose Character
const countChar = (string, char) => {
let result = 0;
for (let i = 0; i < string.length; i++) {
if(string[i] == char){
result++;
}
}
return result;
};
console.log(countChar(“ABBa”, “A”))
//1

1 Like

Exercise 1 - Minimum
function Min(s,t){
return Math.min(s,t)};
undefined
console.log(Min(10,1));
< 1

Exercise 2 - Recursion
// Recursion - isEven function
// 0 is even
// 1 is Odd
// for number n, it’s evenness is same as n-2
function isEven(n){
if (n == 0) return true;
else if (n == 1) return false;
else if (n < 1) return isEven(-n);
else return isEven(n-2);
}
console.log(isEven(50));
console.log(isEven(75));
console.log(isEven(-22));

// Must utilize an increase when pertaining to the negative number.

Exercise 3 - Bean Counting
function countsBs(string){
let count = 0;
for (i = 0; i < string.length; i++) {
if (string[i] === “B”) {
count += 1;
}
}
return count;
}
console.log(countsBs(“BBnnmGHB”));

//2nd part for countchar
function countchar(string, character){
let result = 0;
for (i = 0; i < string.length; i++){
if (string[i] === character){
count += 1;
}
}
return count;
}

1 Like