Chapter 3 Exercises

#205

Minium:
let Output = function Inputs(a,b){
mini = Math.min(a,b);
return mini
}
console.log(Output(0,-5))

Recursion:
/* Output will be called to get the answer*/
let Output = function isEven(N) {
/* N represent the number users will input*/
N = Math.abs(N)
return (N % 2) == 0;
/* When the user enters a number for N /
/
the program will check if there is a reminder*/
/* no remender means N is even*/
function isOdd(n) {
return (N % 2) == 1;}
/* If there is a reminder than the N is odd*/
function reReturn(N){
return isEven(N - 2)
}
}
console.log(Output(75));

Bean Master:
function countChar(string, char) {
let count = 0;
for (let i = 0; i < string.length; i++) {
if (string[i] == char) { count++ }
}

return count;
}

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

}
console.log(countBs(“Barbados”))
console.log(countChar(“Canada”,“a”));

0 Likes

#206

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, "D");

}

console.log(countBs("DAVIDOFIDO"));

// -> 3

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

// -> 4
RECURSION

function isEven(num){

num = Math.abs(num);

if(num === 0)

return "number is even";

else if(num === 1)

return "number is odd";

else

return isEven(num - 2);

}

console.log(isEven(50));

console.log(isEven(75));

console.log(isEven(-2));

Minimum

function min(y,x){

if(y < x) return y;

else

return x;

}

console.log(min(75,105));

//->75

console.log(min(31, 55));

//->31

0 Likes

#207

isEven function to determine whether a number is odd or even

function isEven(N){
if (N==0){
return “Even”;
}
else if (N==1){
return “Odd”;
} else if (N<1){
return isEven(N+2);
} else {
return isEven(N-2)
}
}
console.log (isEven (-11));

0 Likes

#208

ALL WORKING

1. Minimum

function smallestNum(...numbersArray) {
    lowestNumber = true;
    numbersArray.forEach(number => {
        if (lowestNumber == true) {
            lowestNumber = number;
        }
        if (lowestNumber > number) {
            lowestNumber = number
        }
    });
    console.log("Currently the lowest number is " + lowestNumber); // for testing purposes
    return lowestNumber // for production purposes
}

2. Even or Odd

function determineIfEven(num1) {
    switch (!isNaN(num1)) {
        case (num1 == 0):
            console.log(true);
            break;

        case (num1 == 1):
            console.log(false);
            break;

        case (num1 > 1 || num1 < 0):
            if (num1 > 1) {
                while (num1 > 1) {
                    num1 -= 2;
                }
            } else {
                while (num1 < 0) {
                    num1 += 2;
                }            
            }
            determineIfEven(num1); 
        
        default:
            break;
    }
}

3. Bean Counter

function countBs(stringInput) {

    stringInput = String(stringInput);
    let letterArray = Array.from(stringInput), count = 0;

    letterArray.forEach(letter => {
        if (letter == "B" && letter == letter.toUpperCase()) {
            count++;
        };
    });
    return count;
}

function countChar(stringInput, charSelect) {

    stringInput = String(stringInput);
    let letterArray = Array.from(stringInput), count = 0;

    letterArray.forEach(letter => {
        if (letter == charSelect) {
            count++;
        };
    });
    return count;
}
0 Likes

#209

Bean Counter
this one worked for me:

function countBs(string){

let resultB=0;
let resultA=0;
for (let count=0; count<string.length; count++){
string[count]==“b” ? resultA ++ : resultA=resultA;
string[count]==“B” ? resultB ++ : resultB=resultB;
}
return [resultA, resultB];
}
let answer=countBs(“BeansBeansBbbBeans”);
alert(answer);

0 Likes

#210

Minimum

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

Recursion

function isEven(a) {
if (a < 0)
a *= -1;
if(a === 0) {
console.log(‘even’);
} else if(a === 1){
console.log(‘odd’);
} else {
isEven(a-2);
}
}
isEven(-1);

Bean Counting

function countChar(searchString, letter) {
let count = 0
for(let i = 0; i < searchString.length; i++) {
if (searchString[i] == letter)
count++;
}
return count;
}
console.log(countChar(‘Assassination’, ‘a’));

0 Likes

#211

1_ Min

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

  console.log(min(11,12));

2_Recursions

 function isEven(x){
    let num = Math.abs(x);
    if (num == 0) {
      return true;
    }
    else if (num == 1){
      return false;
    }
    else return  isEven (num-2);
  }
  console.log(isEven(-2));

For this exercise I lost myself trying to look for a way to use a for loop to induce recursion. it was thoroughly unsuccessful, I had to look for the answer to move forward.

3_1_

 function countBs(string){
   let count = 0;
   for (let i = 0; i < string.length; i++){
       if (string[i] == "B") {
           count += 1;
       }
   }
   return count;
}

 console.log(countBs("bBBBB"));

3_2_

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

console.log(countChar("foot", "o"));

3_3

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

 function countBs(y){
   return countChar(y, "B")
}

console.log(countBs("bBBBB"));

I struggled ridiculously on this one again, I had to look it up for the first one. then did the second and third by myself.

0 Likes

#212

Minimum

function minimum(n1,n2){
  if(n1 > n2){
    return n2;
  } else {
    return n1;
  }
}

console.log(minimum(10,6));

Recursion

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

console.log(isEven(-50));

Bean Counting
I wanted to try something different from just the for loop, as that seemed to miss the point of the exercise for me considering we just were taught about Recursion. So I used a function’s ability to call on itself, but I struggled to get it done. The issues I had were declaring a global variable and passing ruturn statements from the one function to the other. I had this idea that as long as you have a return in the final function that it would automatically pass to the previous function, this took me 2 hours to figure out that a return need to be placed on the in the countBs() function on the numB() function., but I learned a lot.

var numB = 0;
function numBs(wrds, wLength, numB){
  if(wLength > 0 && wrds[wLength - 1] === "B") {
    numB = numB + 1;
    wLength = wLength - 1;
    return numBs(wrds, wLength, numB);
  } else if(wLength !== 0 && wrds[wLength -1] !== "B"){
    wLength = wLength - 1;
    return numBs(wrds, wLength, numB);
  } else if(wLength === 0){
    return numB;
  }
}

function countBs(wrds){
var wrdsLgth = wrds.length;
return numBs(wrds, wrdsLgth, numB);
}
console.log("Number of Cap B’s: "+ countBs(“BBbbbBBbbb”));

Character Location
This showed me that the previous program I made had some major issues, being the global variable would prevent me for repeating the function without resting it’s values. The solution was the pass the variable instead.

function numBs(wrds, wrdsLgth, numB, kar, numB, sLoc){
  if(wrdsLgth > 0 && wrds[wrdsLgth - 1] === "B") {
    if (wrds[wrdsLgth -1] === kar){
      sLoc = wrdsLgth +" "+ sLoc;
    }
    numB = numB + 1;
    wrdsLgth = wrdsLgth - 1;
    return numBs(wrds, wrdsLgth, numB, kar, numB, sLoc);
  } else if(wrdsLgth !== 0 && wrds[wrdsLgth -1] !== "B"){
    if (wrds[wrdsLgth -1] === kar){
      sLoc = wrdsLgth +" "+ sLoc;
    }
    wrdsLgth = wrdsLgth - 1;
    return numBs(wrds, wrdsLgth, numB, kar, numB, sLoc);
  } else if(wrdsLgth === 0){
    return numB+" Character locations for "+kar+": "+sLoc;
  }
}

function countBs(wrds, kar){
var numB = 0;
var sLoc = “”;
var wrdsLgth = wrds.length;
return numBs(wrds, wrdsLgth, numB, kar, numB, sLoc);
}

console.log("Number of Cap B's: "+ countBs("BBbbbBBbbb", "b"));
console.log("Number of Cap B's: "+ countBs("BBbbbBBbbb", "B"));
0 Likes

#213

Minimum

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

Recursion

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

Bean counting

function countBs(myString) {
        let counter = 0;
        for (var i = 0; i < myString.length; i++) {
          if (myString[i] == "B") {
            counter++
          }
        }
        return counter;
      }

 function countChar(myString, myChar) {
        let counter = 0;
        for (var i = 0; i < myString.length; i++) {
          if (myString[i] == myChar) {
            counter++
          }
        }
        return counter;
      }
0 Likes

#214

function MINIMUM( a , b ){

if ( (a - b) > 0 ){

console.log(+b+ " is the MINIMUM");

}
else if ( ( a -b ) < 0){

console.log(+a+ " is the MINIMUM");

}

else {

console.log(“a and b are equal”);

}
}

0 Likes

#215

function isEven(N){

if ( N === 0 || N === 1) return Boolean(N) ;

else if( N < 0 ) return isEven(-N);

else return isEven( N -2 );

}

0 Likes

#216

function countChar(STRING,CHAR){

var LENGTH = STRING.length ;
var COUNTER = 0 ;

for (i = 0 ; i < LENGTH ; i++ ){

if( STRING[i] === CHAR){
  
  COUNTER ++ ;
}

}

return COUNTER ;

}

console.log( countChar(“BBBBBBAAABBB” , “A” ) );

0 Likes

#217

min( )
This is a trivial problem. Not much to say. I reversed the sense of it just to be different.
GitHub Gist

isEven( n )
The logic on this one’s somewhat contrived (just use if n%2==0). But it’s fairly well laid-out. My only comment here is I see beginners following up a return condition with an else stanza. Fortran-style we omit the else-if nesting nonsense, because the else is that the function continues (77-pun intended). #PoorManSwitch
GitHub Gist

countChar( string, char) + countBs(string)
Again so contrived that I made it count backwards, just for grins.
GitHub Gist

0 Likes

#218
MINIMUM:

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


RECURSION:

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


BEAN COUNTING:

function countBs(string){
   let count = 0; 
   for (let i = 0; i < string.length; i++) {
   if (string[i] == "B") {
   count += 1;
}}
console.log(count);
}


function countChar(string, char){
   let count = 0; 
   for (let i = 0; i < string.length; i++) {
   if (string[i] == char) {
   count += 1;
}}
console.log(count);
}
0 Likes

#219

Minimum

  document.write ("<h2>Minimum</h2>");

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

  var a = prompt("Enter the first number.");
  var b = prompt("Enter the second number.");

  var smallest = min(a,b);
  document.write (smallest + " is the smallest of the two numbers. <br />");

Recursion

  document.write ("<h2>Recursion</h2>");

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

  var even = isEven(a);
  document.write ("It is " + even + " that the first number you entered is even. <br />");

Bean Counting

  function countBs (str){
    var text = str;
    var bCount = 0;
    for (var place = text.length-1; place >= 0; place--){
      if (text[place] == "B"){
       bCount++;
      }
    }
    return bCount;
  }

  function countChar (str, char){
    var text = str;
    var bCount = 0;
    for (var place = text.length-1; place >= 0; place--){
      if (text[place] == char){
       bCount++;
      }
    }
    return bCount;
  }

  var c = prompt("Enter a string.");
  var d = prompt("Enter character to be counted.");
  document.write ("There are " + countChar(c,d) +" " + d + "s in your string. <br />");

0 Likes