Chapter 3 Exercises

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

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

Okay I am struggling to remember all the programming codes is there somewhere where I can reference this

I will however give the exercise a good go.

Hi @PartalosCrypto,

What do you mean by programming codes? Can you be a little more specific.

Thanks. :smiley:

Hi @Malik
I mean the abbreviations like ==, ||, !, +, ++, =+ etc

1 Like

Continuing the discussion from Chapter 3 Exercises:

MINIMUM
var a = 12;
var b = 13;

function min(a, b) {
if (a < b) return a;
else if (a > b) return b;
}
document.write(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);
}
console.log(isEven(50));
console.log(isEven(75));
console.log(isEven(-1));

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

console.log(countBs(“BOB THE BUILDER”));
console.log(countChar(“BOB THE BUILDER”, “E”));

As a side note I believe I understand the functions but as a safety net I am going re-read the chapters as I want to understand the use of the operators.

1 Like

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

2. Recursion
function isEven(a){
if(a == 0){
return true;
}
else if(a == 1){
return false;
}

3. Bean Counting
at this exercise i did not really understand it the first part kind of but the second part of exercise 3 was difficult

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(“BBC”));
// → 2
console.log(countChar(“kakkerlak”, “k”));
// → 4

1 Like

Was struggling to understand this exercise. Really good explanation, step by step. Good job. Thanks :slight_smile:

1 Like
//insert 2 number and give the smaller value

let a1 = prompt("Insert the 1st number");
let b1 = prompt("Insert second number");


a = parseInt(a1);
b = parseInt(b1);

function min(a,b){
    if (a > b) {
        alert(`${a} is bigger than ${b}`);
    } else {
        alert(`${b} is bigger than ${a}`);
    }
}

min(a,b);

//Recursion exercise 

//Define recurisve function isEven corresponding to this description    
//isEven to accept single parameter (positive whole number) and return a Boolean



function isEven(input) {
    input = prompt("Please insert a number");

    if (input < 0){
        console.log(`${input} is not a positive number, please re-enter a new number`)
        isEven(input);

    }
    else if (input % 2 == 0 && input % 2 != 0){
        console.log(`${input} is an even number`)
    } else if (input == 0 ){
        console.log(`${input} is an even number and it is a zero`)
    } else {
        console.log(`${input} is an odd number`)
    }

}

isEven(input);



//Q3 Bean counting

    function countBs(){
        let userInput = prompt("Enter a set of string");
        let counter = 0;
        for (let i=0; i<userInput.length; i++) {

            if (userInput[i]=='B'){
                counter += 1;
            } 
        } 
        console.log(`There is ${counter} of "B"(s) in the string you entered.`);

        }

        countBs();

//Count any char

    function countAnys(){

        let userInput = prompt("Enter a set of string");
        let userInputAlphabet =  prompt("Please enter an alphabet of your choice")  
        let counter = 0;
        for (let i=0; i<userInput.length; i++) {

            if (userInput[i]==userInputAlphabet){
                counter += 1;
            } 
        } 
        console.log(`There is ${counter} of ${userInputAlphabet} in the string you entered.`);

        } 

    countAnys();
2 Likes

1. Minimum

function minimum (a,b){
return (a<=b) ? a : b;
}

2. IsEven

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

3. Bean Counting

function countBs(str){
var count = 0;
for (var i = 0; i < str.length; i++) {
if (str[i] === “B”) {
count++;
}
}
console.log(Number of B in ${str}: ${count});
}
// String inside the console.log() is backtick enclosed.

function countChar(str,char){
var count = 0;
for (var i = 0; i < str.length; i++) {
if (str[i] === char) {
count++;
}
}
console.log(Number of ${char} in ${str}: ${count});
}
// String inside the console.log() is backtick enclosed.

1 Like

Exercise 1

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

console.log(min(5 , 10));
console.log(min(5, -10));

Exercise 2

function isEven(n) {

if (n % 2==0) return true;

else return false;

}

console.log(isEven(50));

console.log(isEven(75));

console.log(isEven(-1));

(and)

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

console.log(isEven(50));

console.log(isEven(-1));

Exercise 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;
}
console.log(countBs(“BBQ”));
// → 2
console.log(countChar(“Character”, “a”));
// → 2

1 Like
function min(t,y){
  return Math.min(t,y);
}
document.write(min(0,10) + "<br>");

document.write(min(0,-10));
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);
  }

}


document.write(isEven(50));
document.write(isEven(75));
document.write(isEven(-4));
function countBs(text){
  counter = 0;
  for (l = 0; l < text.length; l++){
    if(text[l] == "B"){
      counter++;
    }
}
return counter;
}
console.log(countBs("BBFBFBBSJSBB"));

function countChar(text,char){
  counter = 0;
  for (l = 0; l < text.length; l++){
    if(text[l] == char){
      counter++;
    }
}
return counter;
}
console.log(countChar("BBFBFBBSJSBB", "S"));
1 Like

MINIMUM

min

RECURSION

isEven

BEAN COUNTING

countBs

countChar

1 Like

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

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
function min(a, b) {
       if (a < b) return a;
       else return b;
}

console.log(min(1797, 3));
*function isEven(n) {*
* if (n %2 ==0) {*
*  return true*
*} else (n %2 !== 0) *
*    return false*
*  *
*}*


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 countBs(string) {
  
  var counter = 0;
  
 for (n = 0; n <= string.length; n++)
  
  if string[n] ==="B"; {
    
    counter +1;
  }
  return counter;
}

says i am missing an “)”…

1 Like

Hi @jerre,

Please do check your syntax for “for” loop and also “if” statement. You are not using the right syntaxes.

Thanks.

<script type="text/javascript">
       function min(a, b){
         if(a > b){
           return b;
         }
         return a;
       }

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

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

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

       console.log(min(3,6));
       console.log(min(7,2));

       console.log(isEven(0));
       console.log(isEven(1));
       console.log(isEven(-5));
       console.log(isEven(50));
       console.log(isEven(75));
       console.log(countBs("kjhdbkfh;k"));
       console.log(countBs("jbdBo;UbB"));
       console.log(countChar("bfjkuyuGydgwj", "U"));
       console.log(countChar("bfjkuyuUYIUGydgwj", "U"));
    </script>
1 Like

function returnMin (a, b) {
if (a < b){
return a;
}
else return b;
}
console.log (returnMin (8, 4));

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

function countBS(string) {
let result = 0;
for (let i = 0; i < string.length; i++) {
if (string [i] === “B”) {
result = result + 1;

}

}

function countChar(string, character) {

}

console.log(countBS (“BBC”));

1 Like
  1. Minimum
function minimum(x,y){
      if(x<y){
        return x;
      } else {
        return y;
      }
    }
    console.log(minimum(3,5));
  1. Recursion
function isEven(n){
      var num = n*n;
      for (counter=0;counter<(n*n);counter++){
        num-=2;
        if (num === 1){
          return false;
          break;
        } else if (num === 0){
          return true;
          break;
          }
        }
      }
    console.log(isEven(-3));

I don’t know if it’s a good idea, but my thought above was to include negative numbers by multiplying the argument n by itself. The other solution could probably be to use Math.abs(); which should (to my understanding) always return a positive value. If we would like to include just the case of -1, we could probably add another else if -statement. :thinking:

  1. Bean counting
function countChar(str, char){
      var lgt = str.length;
      var amountOfChar = 0;
      for(counter=0;counter<lgt;counter++){
        if (str[counter]==char){
          amountOfChar+=1;
        }
      }
      return amountOfChar;
    }
    console.log(countChar("BarrrBer", "r"));
2 Likes

Minimum

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

Recursion (I had to look up the solution… :zipper_mouth_face:)

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

    }

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 really got stuck on that one. Kind of still am, actually. I had to look it up and am still looking it up.

I’m trying to figure how to code the first part of the exercise where you create a single parametered function to return, in that particular case, uppercase "B"s.

Overall with functions added together I have understood the code. Howerver, singling out just the first part and making it work is still problematic.

2 Likes