Chapter 3 Exercises

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

Exercise 2:
function isEven(n){
if (n == 0) {
return true;
} else if (n == 1) {
return false;
} else if (n < 0){
return isEven(-n);
}else {
//console.log(m)
return isEven(n - 2);
}
}

Exercise 3:
function countBs(word){
let Bs = 0;
for(let i = 0; i < word.length; i++){
if(word[i] == “B”){
Bs += 1;
}
}
return Bs;
}

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

Hey gang! Here are my solutions to the chapter 3 exercises. If anyone needs help or would like me to provide an explanation to my solutions, please feel free to DM me!

Minimum:

  function min(_first, _second) {
    if (_first > _second) {
      return _second;
    }
    else {
      return _first;
    }
  }

// testing (must be in the html script tag) 

let minimum = min(6, 7);
document.write("<h1>" + minimum + "</h1>");

Recursion:

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

// testing (must be in the html script tag) 

document.write("<h1>" + isEven(75) + "</h1>");

If -1 is used, the program hangs. Therefore to solve this issue, Another branch must be added to check whether the number passed is positive or not. Then, the exact same structure above is used but instead of subtracting 2, we add it as seen below.

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

// testing (must be in the html script tag) 

document.write("<h1>" + isEven(-75) + "</h1>");

Bean counting:

function countBs(_string) {
    let end = _string.length - 1;
    let count = 0;

    for (let i = 0; i <= end; i ++) {
      if (_string[i] == "B") {
        count++;
      }
    }
    return count;
  }

// testing (must be in the html script tag) 
  document.write("<h1>" + countBs("BananaBVB") + "</h1>");

  function countLetter(_string, _toBeCounted) {
    let end = _string.length - 1;
    let count = 0;

    for (let i = 0; i <= end; i ++) {
      if (_string[i] == _toBeCounted) {
        count++;
      }
    }
    return count;
  }

// testing (must be in the html script tag) 
  document.write("<h1>" + countLetter("BananaBVB", "n") + "</h1>");
1 Like
  1. Minimum

    function min(a,b){
     return a < b ? a:b;
    }
    console.log(min(-5,9))
    
  2. Recursion

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

    function countBs(str){
        var count = 0;
        str.toLowerCase().split('').forEach(function(item){
            item==='b' ? count++:null;
        })
        return count;
    }
    console.log(countBs("BBC"));
    
    function countChar(str, charac){
        var count = 0;
        str.toLowerCase().split('').forEach(function(item){
            item===charac.toLowerCase() ? count++:null;
        })
        return count;
    }
    console.log(countChar("kakkerlak", "k"));
  1. min function:
function min (x, y) {
    if(x>y) {return y;}
    else {return x;}
}
  1. recursion:
function isEven (val){
    if(val==0) {return true;}
    if(val==1) {return false;}
    if(val>1) {return isEven(val-2);}
    return isEven(val+2);
}
  1. bean counting:
function countChar (char, string) {
    var cnt = 0;
    for(var i=0; i<string.length - 1; i++) {
      if(string[i] == char) {cnt++;}
    }
    return cnt
}

MINIMUM
//function that returns a smaller number
function min(a,b){
if (a<b){
return a;
}
else if (a>b){
return b;
}
else {
document.write(“you picked the same number!”);
}
}
console.log(min(2,3));
//2

RECURSION
//function for checking odd/even numbers
function isEven(x) {
if (x%2==0){
console.log(“the number " + x + " is even”);
}
else{
console.log(“the number " + x + " is odd”);
}
};
console.log(isEven(50));
console.log(isEven(75));

Bean counting
//function that counts “B” characters
function countBs(string){
var count=0;
for (var a=0; a<string.length; a++){
if (string[a]==“B”){
count+=1;
}
}
return count;
}
console.log(countBs(“BabyB”));
//2

2ND PART
//function to counts characters we choose to count
function countchar(string,chooseChar){
var count=0;
for (var a=0; a<string.length; a++){
if (string[a]==chooseChar){
count+=1;
}
}
return count;
}
console.log(countchar(“BabyBy”,“a”));
//1

Great explanations Guactoshi!

Could you pls explain me why in the Recursion exercise, if we do

console.log(isEven(-2));

it will return a true Boolean? (cause in the Exercise Hints they explained that every negative number would return false)

Kudos,
Daniel

Yes, but in the final version we add functionality to use negative numbers by adding the following else if condition. This returns n into the loop as a positive version of itself (the negative of a negative is a positive).

Without this functionality, when a negative number is imputed it will go into an infinite loop creating a more negative number crashing your browser.

1.min function

function min(arg1,arg2) {
  if(arg1 < arg2){
    return arg1
  }else if (arg2<arg1) {
    return arg2
  }else if (arg1 == arg2) {
    return "arg1 == arg2"
  }else {
    return "Error"
  }
}

2.recursion:

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

Minimum
var answer = min(2,9);
function min(a, b){
if (a < b) return a;
else return b;
}
alert(answer);
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(75));
Bean counting A
function countBS(string){
let counter = 0;
for (i = 0; for i < string.length; i++) {
if (string[i] == “B”) {
counter += 1;
}
}
}
Bean counting B
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”);
}
console.log(countBs (“BuBBbamaBBBa”));

  1. Beancounting
function beanCounting(string,char) {
      var count = 0
      for(var index = string.length -1;index >= 0;index--){
        if(string[index] == char){
          count++
        }
      }
      return count
    }

Thank you Bro. I believe you are already a programmer who wants to learn more about smart contracts…am I right/wrong?

Anyway I am from Italy and have a small knowledge about programming languages, but I am really committed to learn more… can I bother you sometimes for some further info if I get stuck?

1 Like

There are by far more experienced developers here. I am just giving an extra push to explaining and breaking down material cleanly. I’ll help however I can anytime brother and I’m sure the community will too.

MINIMUM

// function min returns the minimum of two values

 function min(arg1, arg2) {
    if (arg1 <= arg2){
      return arg1;
    }
    else {
      return arg2;
    }
  }

RECURSION

function isEven(arg) {

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

BEAN COUNTING - Part 1

      function countB(arg) {
      
      /* Write a function countBs that takes a string as its only argument and returns
      a number that indicates how many uppercase “B” characters there are in the string
      */
      
        var numberOfB = 0;

        for (var i = 0; i < arg.length; i++) {
          if (arg[i] == "B") {
            numberOfB += 1;
          }
        }

        return numberOfB;
      }

BEAN COUNTING - Part 2

  function countChar(inString, charToCount) {

    var numberOfCharToCount = 0;

    for (var i = 0; i < inString.length; i++) {
      if (inString[i] == charToCount) {
        numberOfCharToCount += 1;
      }
    }

    return numberOfCharToCount;
  }

  /* Write a function countBs that takes a string as its only argument and
  returns a number that indicates how many uppercase “B” characters there are
  in the string
  */

  function countB(inString) {

    return countChar(inString, "B");

  }

I really appreciated how you broke down the code and explained it. It helped me a lot.

Thx! :slight_smile:

1 Like
	//EX 1
	
	function findMin(a,b)
	{
		if (a > b)
		{
			return b;	
		}
		else if (a < b)
		{
			return a;
		}
		else
		{
			null;
		}
	}
	//END EX 1
	
	//EX 2
	function isEven(a)
	{
		if (a == 0)
		{
			return true;
		}
		if (a == 1)
		{
			return false;
		}
		else if (a > 0)
		{
			return isEven(a-2);
		}
		else if (a < 0)
		{
			return isEven(a+2);
		}
	}
	//END EX 2
	
	//EX 3
	function countBs(string)
	{
		return countChar(string, "B");
	}
	
	function countChar(string, targetChar)
	{
		targetCount = 0;
		for (i = 0; i < string.length; i++)
		{
			if (string[i] == targetChar)
			{
				targetCount++;
			}
		}
		return targetCount;
	}
	
	//END EX 3
	
	//FUNCTION TESTS
	console.log(findMin(9,3));
	console.log(findMin(4,7));
	console.log(findMin(1,1));
	console.log(isEven(50));
	console.log(isEven(75));
	console.log(isEven(-50));
	console.log(isEven(75));
	console.log(countBs("aaaBBBaaBaB"));
	console.log(countChar("aAAaaA", "A"));

Here are my answers to the exercises:
//minimum

function min(a,b){
if (a<b){
return console.log(a + " is the minimum");
}
else if (a>b){
return console.log(b + " is the minimum");
}
else{
return console.log(“There is no minimum”);
}
}

//isEven

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

//Bean Counter

function countBs(a){
let count = 0;
for(x =0; x < a.length; x++){
if(a[x] == “B”){
count++;
}
}
console.log(count);
}

// countChar

function countChar(a,b){
let count = 0;
for(x=0; x < a.length; x++){
if(a[x] == b){
count++;
}
}
console.log(count);
}

// countBs with countChar

function countBs(a){
countChar(a,“B”);
}