Chapter 3 Exercises

Hi @Emmaculate,

Most of the answers on this thread are working programs. For example this -

Please show us what error you’re facing while running the function. Might be a simple issue that’s preventing you from proceeding.

Thanks.

1 Like
  1. Min
    function min(a,b){
      if(a<b){
        return a;
      }else{
        return b;
      }
    }
    console.log(min(3,5));
  1. isEven
    function isEven(x){
      if(x==0){
        return true;
      }else if(x==1){
        return false;
      }else{
        return isEven(x-2);
      }
    }
    console.log(isEven(100));
  1. countChar
    function countChar(str, char){
      var count=0;
      for(let i=0;i<str.length;i++){
        if(str[i]==char) count++;
      }
      return count;
    }
    console.log(countChar("HELLO", "O"));
1 Like

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

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

  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(value1, value2){

      return Math.min(value1, value2);

      }

    • 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, char) {
      let counted = 0;
      for (let i = 0; i < string.length; i++) {
      if (string[i] == char) {
      counted += 1;
      }
      }
      return counted;
      }

      function countBs(string) {
      return countChar(string, “B”);

      }

1 Like
  1. Minimum
function min(x, y){
        return Math.min(x, y)
      }

    console.log(min(8, 12))
// 8
  1. Recursion
<script type="text/javascript">
var numberN = (prompt("Pick a number!"));
var N = numberN

  function isEven(N){
        if  (N == 0){
          return (numberN + " = even");
        }
        if (N == 1){
        return (numberN + " = odd");
        }

        if (N<0){
          (N = N *-2);
          return isEven(N);
        }

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

        }

      }
console.log(isEven(N));
    </script>

With this code, although it works, it gives an Uncaught RangeError: Maximum call stack size exceeded, how would i fix this??

  1. Bean Counting
<script type="text/javascript">


      function countBs(string){

      return countChar(string, 'B');
      }


      function countChar(string, character){
        var result = 0;

        for (var i = 0; i <string.length; i++){
          if (string[i]=== character){
            result = result + 1;
          }

          }

      return result;
    }


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

    </script>

Ohh, thank you for responding. I’ll run it once more and update you with my outcome.

1 Like

It means that the loop kept going on and eventually the stack size was filed up and then the function had to be stopped. Make sure you don’t have a continuous loop running while writing the program.

3 Likes

var X =prompt(“enter a number az an X”);
var Y =prompt(“enter a number az an Y”);
function min(x, y) {
if (x < y) return x;
else return y;}
console.log(“The Minimum inBetween (”+X+"&"+Y +") is "+ min(X,Y));
:grinning:

1 Like

var X =prompt(“enter a number az an X”);
var Y =prompt(“enter a number az an Y”);
function min(x, y) {
if (x < y) return x;
else return y;}
console.log(“The Minimum inBetween (”+X+"&"+Y +") is "+ min(X,Y));

:grinning:

1 Like
  1. 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

  1. Recursion

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

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

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

1 Like

MINIMUM
function smallest(a,b){
if (a > b) {
document.write(b + " is smaller than " + a);
} else if (b > a) {
document.write(a + " is smaller than " + b);
} else if (b = a) {
document.write(a + " is the same number as " + b);
}
}

smallest(-25,25);

RECURSION
function even(original){
console.log(“Your number " + original + " is EVEN”);
}

function odd(original){
console.log(“Your number " + original + " is ODD”);
}

function isEven(n){
var original = n;
if (n> -1){

while (n>1){
  n = n-2;
}
if (n == 0){
even(original);
} else odd(original);

document.write("Your number is " + n);
} else console.log(“I’m sorry,” + original + “is a negative number!”);
}

isEven(-1);

Bean Counting
function countChar(original, char){
var bCounter = 0;
for (counter = 0; counter < original.length; counter++) {
if (original[counter] == char){
bCounter += 1;
}
}
console.log(bCounter);
}

countChar(“kakkerlak”, “k”);

1 Like

function min(a,b){
return “Math.min”;
}
console.log(min(1,2));

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

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

function countChar(string,character){
let count = 0;
for(a=0; a<sting.length; a++){
if(string[a]===“character”){
count++;}
}
return count;
}

1 Like

My code was preety similar to yours on exercise number 3, but cant make it run properly. Would you be kind enough to post this example with a calling function to try to figure out what Im doing wrong? Cheers

Try this instead @Caraenache_Catalin

function countBs(string){
var count = 0;
for(a=0; a<string.length; a++){
if(string[a]===“B”){
count++; }
}
return count;
}

function countChar(string,character) {
var count = 0;
for(a=0; a<string.length; a++) {
if(string[a] === character){
count++; }
}
return count;
}

console.log(countBs(“BBBBBBBBBBBBBBBBBB”));
console.log(countChar(“abcDBAB”, “B”));

1 Like

I was a little stuck on the Bean counting so thanks for the explanation, made it much clearer.

  1. Minimum
    function min(x, y) {
    if (x < y) return x;
    else return y;}
    console.log(min(5, -5));

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

  3. Bean counting
    function countBs(xString) {
    return countChar(xString, ‘T’);
    }
    function countChar(xString, character) {
    let count = 0;
    while (xString.length > 0) {
    if (xString.charAt(0) == character) count++;
    xString = xString.substr(1);
    }
    return count;
    }
    console.log(countBs(“Terrific Taco Tuesday!”));
    console.log(countChar(“Tweeters Tare Trebles.”, ‘i’));

1 Like
  1. function min(a, b) {

if (a < b) return a;

else return b; }

console.log(min(0, 10));

// 0

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

// -10

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

// → true

console.log(isEven(75));

// → false

console.log(isEven(-1));

// → false

  1. 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, “T”);

}

console.log(countBs(“TIM”));

// → 2

console.log(countChar(“timbe”, “t”));

// → 4

1 Like

Very interesting to see how there are so many ways of getting to the same output. Mine are never as efficient as the answers in the book!

Minimum

      //return minimum
      function returnMin(a,b){
        if(a<=b) return a;
        else return b;
      };
      document.write(returnMin(5,8)+"<br/>");

isEven

  //even numbers
           function isEven(x){
               if (x%2==0) return true;
               else if (x%2==1) return false;
               else if (x<0) return isEven(-x);
               else return isEven(x-2);
               };
               document.write("Result of isEven: " + isEven(-5));

Bean Counting

function countBeans(word) {
            var L;
            var beans = 0;
            L = word.length;
          // P is the position in the word
            for (P=0; P<L; P++){
              if(word[P]=="B"){
                beans++;
              }
              }
              return beans;
            }
document.write("The number of Beans is " + countBeans("BBBBBR") + "<br/>");

Char Counting - taking character and word from user

          var word = prompt("Enter a word");
          var charToCount = prompt("Enter the character to count in the word");
          
          function countChar(charToCount, word) {
            var L;
            var charCount = 0;
            L = word.length;

          // P is the position in the word
            for (P=0; P<L; P++){
              if(word[P]==charToCount){
                charCount++;
              }
              }
              return charCount;
            }

          document.write("The number of " + charToCount + "'s is " + countChar(charToCount, word) + "<br/>");
2 Likes
Minimum:

  function min(x,y){
    if (x<y) {
        console.log(`The minimum number is ${x}`);
    } else {
        console.log(`The minimum number is ${y}`);
    }
  }

  let firstNumber = parseInt(prompt("Enter the first number please:"));
  let secondNumber = parseInt(prompt("Enter the second number please:"));

  min(firstNumber,secondNumber);


Recursion:

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

  let number = parseInt(prompt("Enter the number please:"));
  let userNumber=number;

  if (isEven(number)) {
    console.log("The number "+userNumber+" is even");
  } else {
    console.log("The number "+userNumber+" is odd");
  }


Bean counting:

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

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

  let string = prompt("Enter the word please:");
  let character = prompt("Enter the character that is to be counted please:");

  console.log(`The word `+string+` as ${countBs()} uppercase "B" characters`);
  console.log(`The word `+string+` as ${countChar(string,character)} "`+character+`" characters`);
1 Like

Minimum:

function min (a, b) {
          if (a > b) return b + " is smaller than " + a;
          else return a + " is smaller than " + b;
        }

Recursion

      function isEven(a){
          if (a == 0) return a + " is an even number";
          else if (a ==1) return a + " is an odd number";
          else if (a < 0) return "Type in a positive number!";
          else return isEven (a - 2);
              }

Bean counting

 function countBs (string){
   for (x=0; x < string.length; x++){
     if (string[x] == "B")(ub += 1);
   }
 return "There are " + ub + " uppercase B letters";
 }

countBs("Bomberol");
var target = 0;
function countChar (string, char){
  for (x=0; x < string.length; x++){
    if (string[x] == char)
    (target += 1);
  }
return "There is" + target + ' ' + char + " characters";
}

countChar("Bomberol", "o");
1 Like