Chapter 3 Exercises

const mathMin = function(a , b){

if(a < b){

    return a;

}else{

    return b;

}

}

const isEven = function(a){

if(a == 0){

    return true;

}else if(a<0){

    return false;

}else{

    return isEven((a - 2));

}  

}

const countBs = function(string){

var counter = 0;



for(var i = 0; i < string.length;i++ ){

    if(string[i] == "B"){

        counter++;

        }

}

return counter;

}

const countChar = function(string, char){

var counter = 0;



for(var i = 0; i < string.length;i++ ){

    if(string[i] == char){

        counter++;

        }

}

return counter;

}

1 Like

My code. Again probably not as efficient as some, but its how im applying my knowledge at this stage.

  1. min function:

function min(a, b) {
if (a < b) {
return a;
} else if (a > b) {
return b;
} else {
return “Equal value”;
}
}

  document.write(min(6, 14));
  1. Recursion:

function isEven(N) {

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

  var isItEven = isEven(prompt("Even number? tester.", "Enter a number"));

  document.write(isItEven);
  1. Bean Counting: (the user defined character version)

function countChar(userCHAR, userSTR) {
let userCharCount = 0;

    for (let charCount = 0; charCount < userSTR.length; charCount++) {
      if (userSTR[charCount] === userCHAR) {
        userCharCount++;
      }
    }
    return userCharCount;
  }

  var howMany = countChar(
    prompt("Count which character?", "Enter a character"),
    prompt("Count how many in what string?", "Enter a text string")
  );
  document.write(howMany);
1 Like

Minimum

function min(num1, num2) {
   if (num1 < num2) {
      return num1;
   } else {
      return num2;
   }
}

Recursion

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

Bean Counting

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

I had to rely on the solution on the third one (Bean Counting), a difficult one for my current level, but I am hopeful to improve along the way of the course! :sweat: :grin:

1 Like

I like your code, so just keep it up! :slightly_smiling_face::+1:
Ivo

1 Like

It’s ok to look at the solution if we have tried our best. That happens to the best of us. But then it’s importent to read the code and understand the solutuion you used…
Good job. :+1:
Ivo

1 Like

Thanks for your input and your advice Ivo! :+1:

1 Like

01.) Minimum

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

02.) Recursion

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

or

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

//03.Bean counting

function countBs(stringB){
var numberOfBs = 0;
	for (i=0; i<stringB.length;i++) { 
		if (stringB[i] == "B") {
			numberOfBs = numberOfBs + 1;
		}		
     }
return numberOfBs
}
function countChar(stringN,charToCount){
	var numberOfChars = 0;
	for (i=0; i<stringN.length;i++) { 
		if (stringN[i] == charToCount) {
			numberOfChars = numberOfChars + 1;
		}		
     }
return numberOfChars
}
2 Likes
  1. Minimum
function min(a, b) {
    if (a < b) return a;
    else return b;
  }
  1. Even or Odd
function isEven(x) {
  if (x>=0 && x%2==0) return "true";
  else if (x<0) return "negative";
  else return "false";
}
  1. Bean
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");
  }
1 Like

Function 1. This first exercise was easy. I did it on the first attempt and was so shocked!!

1 Like

1 Like

Hi @NeilHangzhou1

You can copy past your code on the page with the Preformatted text balise it will be easier to read your code :wink:

3.Counting Beans Exercise

function countBs (str){
let count = 0;
for (var i = 0; i < str.length; i++){
if (str [i] == “B”) {
count += 1;
}
return count;
}
}
document.write (countBs(Big Boobies))

Can’t understand why I keep on getting this error statement when I run this code:
“missing ) after argument list”. Been taking away/adding semicolons/different kinds of brackets for hours and can’t work it out (just as with an exercise from the last chapter, still uncompleted).

Thanks, gabba…

However, can’t work out how to get the colours within the developer console or from Atom editor to paste into here or onto to document. :grin: :grin:

Minimum
function returnMinimum(x, y){
if(x < y){
return x;
}
else{
return y;
}
}
console.log(returnMinimum(25, 30));

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

beanCounting

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

1 Like

Exercise 1:

function min(n1,n2){
          if (n1 == n2){
            console.log("Please give two different numbers...");
          }else if (n1 > n2){
            return n2;
          }else return n1;
          }

Exercise 2:

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

Exercise 3:

function countChar(textToCheck, char){
          let bCount = 0;
          for (let i = 0 ; i < textToCheck.length ; i++){
            if (textToCheck[i] == char) bCount++;
          }
        return bCount;
        }
        function countBs(string){
          return countChar(string, "B");
        }
2 Likes

//Minimum
function min(_first, _second){
if (_first <= _second) return _first;
else return _second;
}

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

//Bean Counting
function countChar(_theString, _targetChar){
let _charCount = 0;
for (counter = 0; counter < _theString.length; counter++){
if (_theString[counter] == _targetChar) _charCount++;
}
return _charCount;
}
//Test
console.log(countChar(“IvanOnTech”, “S”));
console.log(countChar(“IvanOnTech”, “n”));
console.log(countChar(“IvanOnTech”, “T”));

2 Likes

function min(a, b) {
if (a < b) return a;
else return b;
}
console.log(min(9, 10));
// → 0
console.log(min(0, -1));
// → -10

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

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

1 Like

//Min
function min(a, b){
if (a < b)
return a;
else
return b;
}
console.log(min(2,4));

//Even
function even(a){
if (a % 2)
return false;
else
return true;
}
console.log(even(75));

1 Like

//Bean Counting

	function countBs(string) {
       	var bsCounter = 0;
        for (var counter = 0; counter<string.length; counter++){
            if (string[counter] === "B"){
                bsCounter++;
            }
        }
        return bsCounter;
    }

    console.log(countBs("Blockchain Bob and Boobs"));
1 Like

Chapter 3: Minimum

const min = (a, b) => {
	return Math.min(a, b);
}

Chapter 3: Recursion

function isEven(val) {
	if (val == 0)
		return true;
	if (val == 1 || val < 0)
		return false;

	return isEven(val - 2);
}

Chapter 3: Bean Counting

function countChar(string, char) {
	let str = String(string),
		count = 0;

	for (i = 0; i < str.length; i++) {
		if (str[i] === char) {
			count++;
		}
	}

	return count;
}
1 Like