Chapter 3 Exercises

// find minimum

function minimum(a,b){

  if (a < b){



    return a;
  }

  else {


    return b;
  }

}

alert(minimum(9,1));


// recursion

function even_num(n){
  if(n == 0) return true;
  if(n == 1) return false;
  if( n < 0) return "Negative integer";
  else return even_num(n-2);




}


console.log(even_num(50));
console.log(even_num(75));
console.log(even_num(-1));


// counting beans


function countBs(string){

  return countChar(string, "B");
  // creat count and set to 0;

 }

 function countChar(string, character){

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

     if(string[i] == character){
       count++;
     }
   }

  return count;
 }



console.log(countBs("ABBIBKAF"));
console.log(countChar("KKkAbcFIA", "A"));
1 Like

Hi!
Try pressing the ‘space bar’ at least four times before each line. The code should apear nicely. But you still have to indent it manually. Hope it works!

Ch 3 Ex 1

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

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

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

1 Like

The idea of using loops is starting to make sense. The way to call a function to do another function is interesting, yet challenging. I am still pushing forward to complete the course no matter what.
Minimum
function min(a,b){
if (a<b)
return a;
else return b;
}
document.write(min(20,13));
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(-2));

Bean Counting
function countBs(string){
return countChar(string, ‘B’);

}

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

}

}
return result;
}

console.log(countBs(“BBBBBBBBBBBBBBBBBBBB”));
console.log(countChar(“lkdsjaflajdaaaaaaj”, “a”));

2 Likes

Thanks for the detailed explanation.

Exercise 1 - Minimum:

<meta charset="utf-8">

<title>This is a great website</title>
<h1> Exercise 1 - Minimum </h1>

<script>

    function min (x, y){

    if (x < y)

      return x;

  else
    return y;



    }

console.log(min(2,4))

</script>

Exercise 2 - Recursion:

<meta charset="utf-8">

<title>This is a great website</title>
<h1> Exercise 2 - Recursion </h1>

<script>

function isEven (n) {

  if (n == 0){
  return true;

}
else if (n == -1){

  return false;

}
else {return isEven (n - 2)}

}

console.log (isEven(75));

</script>

Exercise 3 - Been Counting (Had trouble with this one :confused: )

<meta charset="utf-8">

<title>This is a great website</title>
<h1> Exercise 2 - Recursion </h1>

<script>

function countChar(string, character) {
  let counter = 0

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

    if (string[wordCount] == character)

    counter += 1;
  }

  console.log (counter);
}

function countBs(string)
{

  return countChar(string, 'B')

}

countBs("Banana")



</script>
1 Like
function countBs(x){
    var y=0; 
  for(tracker=0 ;tracker<=x.length; tracker++){
      let toReturn= x[tracker];
      if(toReturn!="B"){console.log();}
      else{(y += toReturn.length)};
    }
    return y;
}
countBs("BigBenBrownBeansbbbBBB");

/* Rewriting the countBs function*/
function countChar(x,z){
    var y=0; 
  for(tracker=0 ;tracker<=x.length; tracker++){
      let toReturn= x[tracker]; 
      if(toReturn!=z){console.log();}
      else{(y += toReturn.length)};
    }
    return `${y} ${z}`;
}
countChar("BigBenBrownBeansbbbBBB","B");
1 Like

//
//minimum
//
function minTest2Numbers(enterFirst,enterSecond) {
alert(“Min of " + enterFirst +”, “+ enterSecond +” is " +
Math.min(enterFirst,enterSecond));

  }
   minTest2Numbers(5,10);

//isEven
function isEven(enterWholeNbr) {
if (enterWholeNbr < 0) {
enterWholeNbr = Math.abs(enterWholeNbr);
}

        if (enterWholeNbr ===0) {
              console.log("is " +  enterWholeNbr + " even? true")
            }

        if  (enterWholeNbr ==1) {
              console.log("is " +  enterWholeNbr + " even? false")
              }

              else {
                enterWholeNbr = enterWholeNbr -2;
                console.log("is " +  enterWholeNbr + " even? false")
              }

            }
            console.log(isEven(75))
          // isEven(3);

//
//BeanCounting countBs
//
function countBs(enterString) {
var count = 0;
var countUpper = “B”;

      for (i = 0; i < enterString.length; i++) {
        console.log(enterString[i]);

     }
     return count;

    }
    console.log(countBs("Bye Good Bye"));
    //console.log(count);

//
//BeanCounting countChar
//
function countChar(enterString, enterLetter) {
var count = 0;
var Letter =enterLetter;

      for (var i = 0; i < enterString.length; i++) {
        if (Letter.indexOf(enterString[i]) > -1) {
          count++;
        }
      }
      return "This string  has, " + count + " "+ enterLetter + " Letter(s)";

  }
  //countChar("ABCDBBBBB", "B");
  console.log(countChar("ABCDBBBBB", "A"));
1 Like

Sorry I do not have any advice but just letting you know you are not alone. These exercises are very hard for someone without any previous knowledge. Let’s keep trying!!

1 Like

Function 1 Math Minimum Recreation:

function returnMin( a, b, c)
{
    if(a < b && a < c)
    {
        console.log(a);
    }
    else if(b < a && b < c)
    {
        console.log(b);
    }
    else if (c < a && c < b)
    {
        console.log(c);
    }
    else console.log("Error Give me numbers that are not equal to each other!");

}

Function 2: Recursion

function isEven(number)
{
    if(number == 0)
    {
        console.log( " this number is even ");
        return true;
    }
    else if (number == 1)
    {
        console.log( " this number is odd ya dingus ");
        return false
    }
    else if (number < 0)
    {
        console.log(number + "Will be changed to a positive inteager so your number can be checked ");
        number = -number;
        return isEven(number);
    }
    else

Function 3: Bean Counting

var capitalBs = 0;

function countBs(text)
{
    for(counter = 0; counter <= text.length; counter++)
    {
        if(text[counter] == "B")
        {
            capitalBs++;
        }
    }
}
// Idk why but when this function is executed without being used by countBs it only ever returns 1.
function countChars(text, char)
{

let charAmmt = 0;

    for(counter = 0; counter <= text.length; counter++)
    {
        if(text[counter] == char)
        {
            charAmmt++;
        }
    }
    return (charAmmt);
}

countChars("NeverGonnaGiveUup");

function countBs(text)
{
    return countChars(text, "B");
}

countBs("Boy Being Blue");
1 Like

Thanks for your encouragement Crypila, much appreciated!

Hope our efforts will pay off soon :nerd_face:

1 Like

MINIMUM

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

document.write (min (0, 20));

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));
console.log(isEven (75));
console.log(isEven (-1));

BEAN COUNTING

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

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

    console.log(countChar("kakkerlak", "k"));

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

Minimum

function minimum(a,b){
      return Math.min(a,b);
    }
    console.log(minimum(23,400));

Recursion

function isEven(number){
      let even = 0;
      let odd = 1;
      if(number == even) {
        return true;
        }
        else if (number == odd) {
          return false;
          }
          else {
            return isEven(number-2);
          }
        }
      console.log(isEven(50))

using -1 the program is an infinite loop, since 0 or 1 which mark the end of the program are never reached by substracting -2 from -1.

By using the the second funtcion “positiv” every number, all negative numbers are converted to positiv number. If this function is included in the “isEven” function, this also works for negative numbers.

function positiv(number){
      if (number > number*(-1)) {
        return number;
      }
        else {
          return number*(-1);
        }
      }
    console.log(positiv(-10));

    function isEven(number){
      let even = 0;
      let odd = 1;
      if(number == even) {
        return true;
        }
        else if (number == odd) {
          return false;
          }
          else {
            number = (positiv(number));
            return isEven(number-2);
          }
        }
      console.log(isEven(-200))

Bean Counting

function countBs(text){
        let upperCaseB = 0;
        for(counter = 0; counter <= (text.length-1); counter++){
          if (text[counter] == "B") {
          upperCaseB ++;
          }
            else {
            }
        }
        return upperCaseB;
      }
console.log(countBs("aBcBa"));

modified for any char:

function countChar (text, char){
      let anychar = 0;
      for(counter = 0; counter <= (text.length-1); counter++){
        if (text[counter] == char) {
        anychar ++;
        }
          else {
          }
      }
      return anychar;
  }
console.log(countChar("aBcBa", "a"));
1 Like

Quick question about the bean counter exercise, if someone cares to help:
Is there a way to program the function so that it will accept a string as an argument without having to enter it in quotes, for example countChar(Bean, B) instead of countChar(“Bean”,“B”)?

Beans:

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

Minimum:

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

Recursion:

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

//Minimum
function minus(a,b){
if(b === undefined) return -a;
else return a-b;
}

console.log( minus(10));

console.log(Math.min(10, 5));Preformatted text

1 Like

//Minimum
function minus(a,b){
if(b === undefined) return -a;
else return a-b;
}

console.log( minus(10));

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

1 Like
  1. Minimum

function min(x, y){
if (x < y) return “x is smaller than y”;

  else {
    return "y is smalle than x";

  }
}
var  answer = min (32,22);
alert(answer);
  1. Recursion

function isEven(x){
if (x < 0){
return isEven (-1 * x);
}
if (x == 0){
return true;
}
else if (x == 1){
return false;
}
else {
return isEven(x-2);
}
}
var answer = isEven(8);
alert(answer);

  1. Bean counting

function countBs(stringIn, letter)
{
var letter_Count = 0;
for (var position = 0; position < stringIn.length; position++)
{
if (stringIn[position] == letter)
{
letter_Count += 1;
}
}
return letter_Count;
}

var answer = countBs(“BaBy”, “y”);
alert(answer);

1 Like

function isEven (number) {
if (number == 0) {
return true;
}
else if (number == 1) {
return false;
}
else if (number <0) {
return isEven (number+2) }
else{
return isEven (number-2) }
}
console.log(isEven(50));
console.log(isEven(75));
console.log(isEven(-1));

1 Like

Minimum

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

Recursion

I managed to write a code for it without using recursion, but to do it with recursion I had to look for help. I'll post the code without recursion since the other one is not really my code.
 function isEven(n)
 {
   for(n; n>=2; n-=2)
   {
     console.log(n);
   }

   if (n==1){document.write("odd");}
   else if (n==0){document.write("even");}
   else document.write("error");
 }

Bean Counting

    var amountCharacters=0;
    var character2 =0;
   function countChar(character, string)
   {
     let N = string.length -1;
     character2=character;
     for(amountCharacters=0; N >= 0; N-=1)
     {
       if (string[N] == character) {amountCharacters+=1};
     }
     return amountCharacters;
     return character;
   }
   countChar("F", "FFafFuF");
   document.write("There are " + amountCharacters + " " + character2 + " letters" + "<br>");
1 Like