Chapter 3 Exercises

countBs

function countBs(word){
var numberOfB = 0;
for (count = 0; count < word.length; count ++){

if (word[count] == "B"){

  numberOfB +=1;
}

}
return numberOfB;
}
var answer = countBs(“My name is Jebel without a capital B”);
document.write(answer);


countChar

function countChar(word,char){
var numberOfChar = 0;
for (count = 0; count < word.length; count ++){

if (word[count] == char){

  numberOfChar +=1;
}

}
return numberOfChar;
}
var answer = countChar(“My name is Jebel without a capital”,“M”);
document.write(answer);

1- Minimum
function min(a,b){
if(a>b) a=b;
return a;
}

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

   return ret;
 }

3–Bean counting
3-1
var count=0;
function countBs(str){
for(let counter=0;counter<=str.length-1;counter++){
if(str[counter]==“B”)
count++;
}
return count
}

3-2-
var count=0;
function countBs(str,char){
for(let counter=0;counter<=str.length-1;counter++){
if(str[counter]==char)
count++;
}
return count
}

Simply put a return behind isEven(x) in line 5 and it would work for numbers bigger than 0.
return isEven(x).

1. Minimum

               function min(x, y) {
			if (x < y) {
				return x;
			} else if (y < x) {
				return y;
			} else {
				document.write("Given numbers are equal");
				return x;
			}
		}

2. Recursion

		function isEven(x) {
			if(x < 0) {
				return "Please provide a positive whole number";
			} else if (x.toString().indexOf(".") >= 0) {
				return "Please provide a positive whole number";
			} else if (x == 0) {
				return true;
			} else if (x == 1) {
				return false;
			}
			return isEven(x - 2);
		}

3. BEAN Counting

		function countBs(str) {
			var counter = 0;
			const letter = "B";
			for (var i = 0; i < str.length; i++) {
				if (str[i] == letter) {
				 counter++;
				}
			}
			return counter;
		}
		
		
		function countChar(str, findChar) {
			var counter = 0;
			for (var i = 0; i < str.length; i++) {
				if (str[i] == findChar) {
				 counter++;
				}
			}
			return counter;
		}

Thanks a lot! Worked like a charm!

function min(a,b) {
  if (a>b) {
	return b;
  } else {
	return a;
  }
}
function isEven(n) {
  if (Math.abs(n)===0) {
	return true;
  } else {
	if (Math.abs(n)===1) {
	  return false;
	} else {
	  return isEven(Math.abs(n)-2);
	}
  }
}
    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;
    }

function min(a,b){

return Math.min(a,b);

}

console.log(min(3,5));

Math Min:
var n1= 1, n2= 50, n3= 100;
var answer = Math.max (n1, n2);
var answer2 = Math.min (n1, n2);
document.write("Max is: " + answer);
document.write("
Min is: " + answer2);
answer = Math.max(n1, Math.max(n2, n3));
document.write("
Max between three is: " +answer);

Recursion:
function isEven(i){
var s =false;

if(i<0){
console.log(“Enter positive number”);
return;
}
else if(i==0);
s=true;
else if(i==1);
s=false;
else
ret=isEven(i-2);
}
return s;
}

Having a little bit of trouble with this one, where are my errors?

Bean Counter:
function countBs(string){
for (let counter=0; counter<=string.length-1; counter++){
if(string[counter]==char)
count++;
}
return count
}

I think I have a good grasp of the third. Please let me know if you find any errors in my answers.

Minimum

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

Recursion

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

Bean Counting

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

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

Minimum:

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

}

min();

isEven:

function isEven(N)
{

var result = N;
if(N==0)
{
console.log(“Is Even”);
} else if(N==1)
{
console.log(“Is Odd”);
}
else if(N<0)
{
isEven(-N);
}
else
{
isEven(N-2);

  }

}

isEven(8);

Here are my solutions to the chapter 3 exercises…

Minimum

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

Recursion

function isEven(number) {

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

	console.log("Not a positive, whole number");
}

Bean Counting

function countChar(word, char) {

	let count = 0;

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

	return count;
}

Bean Counting

function countBs(word, char)
{

var numlet = word.length;
var numB = 0;

for(var count = 0; count <numlet; count++)
{
if(word.charAt(count) == char)
{
numB += 1;
}

}

console.log(numB);

}

countBs();

// Thought I would never get this, glad I did

//1-Minimum
function min (a , b) {
return Math.min(a,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);
}
}

//counting letters
function countBs(string , letter) {

var toCount = 0;

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

    if (string[i] == letter) {

        toCount++;
    }
}

return toCount; 

}

Fun exercises :slight_smile: Here’s mine…

Minimum

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

console.log(min(0, 10));
// → 0
console.log(min(0, -10));
// → -10

Recursion

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));
// → true
console.log(isEven(75));
// → false
console.log(isEven(-10));
// → ??

Bean Counting

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

function countChar(text, char) {
  counter = 0;
  for(letter = 0; letter < text.length; letter++) {
    if(text[letter] == char) {
      counter++;
    }
  }
  return counter;
}

console.log(countBs("BBC"));
// → 2
console.log(countChar("kakkerlak", "k"));
// → 4
1 Like

Minimum

// Your code here.

const min = (value1, value2) => value1 < value2 ? value1 : value2;

console.log(min(0, 10));
// → 0
console.log(min(0, -10));
// → -10

Recursion

// Your code here.

const isEven = number => {
  if (number < 0) number = number * -1;
  
  if (number === 0) {
    return true;
  } else if (number === 1) {
    return false;
  } else {
  	return isEven(number - 2);
  }
};

console.log(isEven(50));
// → true
console.log(isEven(75));
// → false
console.log(isEven(-1));
// → ?? 

Bean counting

// Your code here.

const countBs = string => countChar(string, 'B');

const countChar = (string, target) => {
  let count = 0;
  for (let char of string) {
    if (char === target) count++;
  }
  return count;
};

console.log(countBs("BBC"));
// → 2
console.log(countChar("kakkerlak", "k"));
// → 4 
2 Likes

Here are my answer -

Minimum


function min(x,y) {

  return (x>y? y: x);


}

console.log("min 4 5 = " + min(4,5));
console.log("min 9 3 = " + min(9,3));
console.log("min 6 6 = " + min(6,6));

Recursion


function isEven(N) {

  N = Math.abs(N);

  if (N == 0) {
            return true;
  }

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

  if(N>1) {}
  return  isEven(N-2);

}

console.log("is -22 even " + isEven(-22));

Bean Count


function beancount(st,stfind) {

  var count = 0;

for (var i = 0; i < st.length; i++) {
  if(st[i]==stfind){ count++ }
}

return count;

}

console.log("this is a test in javascript " + beancount(“this is a test in javascript”,“a”));


Min

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

Recursion

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

Bean counting
hmm the EOS launch video has priority :slight_smile:

1 Like

Yes, I did it! :baby_chick:

4 Likes

MinVal
// function that returns the lowest value of the parameters given.

function min(val1, val2){
if (val1 < val2){
return val1;
} else {
return val2;
}
}

is Even

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

countBs and charCount

// count the B characters of a “Bean”
function countBs(string = “Bean”){
let count = 0;
for (n = 0; n <= string.length; n++){
if (string[n] == “B”){
count++;
}
}
return count;
}

// count how many times a char appears in a string
function countChars(string, char){
let count = 0;
for (n = 0; n <= string.length; n++){
if (string[n] == char){
count++;
}
}
return count;
}

  1. function returnMin(a, b){
    if(a<b){
    return a;
    }
    else if (b<a){
    return b;
    }
    else{
    return a;
    }
    }

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

  3. function countChar(word, letter){
    var counter = 0;
    for(var i = 0; i < word.length; i++){
    if(word[i] == letter){
    counter++;
    }
    }
    return counter;
    }

1 Like