Chapter 3 Exercises

1.- I went ahead and did something similar to the solutions on the forum because the sturtuire that i used was not accurate:
function min (a,b){
if (a>b) return a; else return b;
}
console.log (min (3,9));
2.- I wrote the following but realized that there was a statement before hand with the 0 is even etc:

function isEven (a,b) {
if (a,b %2); return (“It is an even number”);}

console.log (isEven(50,75));

3.- Copied it from @jgmusso:
var str = “B + + +sws BBB dqdBBB”;
var search = “B”;
alert(countUpper(str, search));

function countUpper(stri, lett) {
var count = 0;
for (var i = 0; i < stri.length; i++) {
if (stri[i] === lett) {
count += 1;
}
}
return count;
}

1 Like

Splendid! Keep up the great work. :smiley:

1 Like

Minimun

function minus(x,y){
return Math.min(x,y);
}
console.log("The smaller between 12 and 6 is " + minus(6,12))

Recursion

function isEven(num) {
if (num < 0){
return “num must be positive”;
}else if (num % 2){
return “even”;
} else return “odd”;
return isEven(num - 2);
}
console.log(isEven(-1));

Bean counting

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

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(“BBCBBBB”));
// →6
console.log(countChar(“kakkkerlak”, “k”));
// → 5

1 Like
  1. Minimun.
    First solution:
    function min (a,b){
    if(a<b);
    return a;
    if(a>b);
    return b;
    }
    console.log(min(4,5));

    Second Solution:
    function min (a,b){
    return Math.min(a,b);
    }
    console.log(min(9,55));

  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 countChar(str, chr)
{
let count = 0;
for (let i = 0; i < str.length; i++) {
if (str[i] == chr)
count++;
}
return count;
}

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

1 Like
// Minimum
function min(num1, num2) {
    if(num2 === undefined) {
        return num1;
    } else {
        if(num1 >= num2){
            return num1;
        }
        else {
            return num2;
        }
    }
}
console.log(min(23,-45));


// Recursion
function isEven(n) {
    if(n < 1) { // My take on preventing a stack overflow
        if(n === 0) return "You must enter a non-zero number";
        n = -n;  // even negative numbers can be used to determine if  odd/even
    }

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

console.log(isEven(-21));


// Bean counting
let sentence = "Bitcoin Blockchain beats centralization";

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

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

console.log(countBs(sentence));
console.log(countChar(sentence, 'i'));
1 Like

I am again having trouble with one of the exercises (What’s new…). This time it’s the recursion one. My thought process for this one was to first break down the given number to a “simplifiedVersion” (which would be 0 or something else) by continually subtracting 2.

Then based on the value of simplifiedVersion I created an if else conditional program to output true or false. Here is the code, any help would be much appreciated:

 fucntion isEven(a){

        var simplifiedVersion = a

        while(simplifiedVersion >= 0){
          simplifiedVersion -= 2
        }

        if(simplifiedVersion = 0){
           let result = true
        }
        else{
        let result = false
        }
        return result
      }
        Var evenOrNot1 = isEven(50)
        Var evenOrNot2 = isEven(75)

          alert(evenOrNot1)
          alert(evenOrNot2)

Im confused AF, any help is appreciated.

MINIMUM

function min(a,b){
let test = a-b
if (test > 0) answer=b;
else if (test < 0) answer=a;
else if (test == 0) answer="Same number";
else answer="NaN";

return answer;
}
min (5,4);
alert ("Answer is " + answer);


RECURSION

let N=Number(prompt("Enter number:"));

function isEven(N){
if (N==0) result="True";
else if (N==1) {result ="False";}
else if (N<0) {N=-N;isEven(N);}
else {N=N-2; isEven(N);}

return result;
}
isEven (N);
document.write("The number is Even: " + result);


B COUNTING

let string=prompt("String");
let char=prompt("Character");
let counter=0;

function countChar(string, char){
  let stringLength=string.length;

  for(let n=0;n<stringLength;n++){
    if (string[n]==char) counter++;
  }

return counter;
}
countChar(string,char);
document.write(counter);
1 Like

Your logic is on point, its really just syntax issues (which is the easier thing to learn, the logic is the key thing). You can copy paste the following into web console and it will be easier to read and to to run.


function isEven(a){  //Corrected spelling of function

      var simplifiedVersion = a;
    
       while(simplifiedVersion > 0){   //changed >= to >
         simplifiedVersion -= 2;
       }

       if(simplifiedVersion == 0) //changed = to == since its an equivalency query, removed {} and let
           result = "true" //added quotations
       else{
          result = "false" //added quotations and removed let
       }
       return result
     }
    isEven(50);alert (result)
    isEven(75);alert (result)  //alerted the result, not the function itself

1, min.math

function min(a, b) {

if (a < b) return a;

else return b;

}

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

// → 0

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

// → -10.
0
-10

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

// → true

console.log(isEven(75));

// → false

console.log(isEven(-1));

// → false
true
false
false

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
2
4

1 Like

MINIMUM
function min(a,b){
if(a < b) return a;
else return b;
}
console.log(Math.min(2, 4)); // -> 2
console.log(Math.min(2, -4)); // -> -4

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

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(countBs("NothingHere"));   // -> 0
  console.log(countBs("Before"));   // -> 1
  console.log(countBs("BBB"));   // -> 3
1 Like

Thanks for the corrections man i greatly appreciate it! I can’t believe i messed up the spelling of function haha. Also, do we always use quotations for booleans? I forgot.

1 Like
  1. MINIMUM
    Write a function min that takes two arguments and returns their minimum.
function min(a,b){
  return a < b ? a:b;
}
console.log(min(0, 10));
console.log(min(0, -10));
  1. RECURSION
    Define a recursive function isEven corresponding to this description. The
    function should accept a single parameter (a positive, whole number) and return
    a Boolean.
    Test it on 50 and 75. See how it behaves on -1. Why? Can you think of a
    way to fix this?
function isEven( number ) {

  	if ( number < 0 )
      number *= -1;

  	if ( number == 0 ) {
      result = true;
    }

    else if ( number == 1 ) {
      result = false;
    }

    else {
      number = number - 2;
      isEven( number );
    }
  return result;
}

console.log(isEven(50));
console.log(isEven(75));
console.log(isEven(-1));
  1. BEAN COUNTING
    Write a function countBs that takes a string as its only argument and returns
    a number that indicates how many uppercase “B” characters there are in the
    string.
    Next, write a function called countChar that behaves like countBs, except
    it takes a second argument that indicates the character that is to be counted
    (rather than counting only uppercase “B” characters). Rewrite countBs to
    make use of this new function.
function countBs(s) {
  var count = 0;
  for (var i = 0; i < s.length; i += 1) {
    if (s.charAt(i) === "B")
      count += 1;
  }
  return count;
}

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

console.log(countBs("BillyBee"));
// 2
console.log(countChar("BillyBee", "b"));
// 0
1 Like
function min(x,y) {
    if(x<=y){
        return x;
    } else {
        return y;
    }
}
console.log(min(5,10));
// -> 5
console.log(min(1,-3));
// -> -3
console.log(min(3,3));
// -> 3
function isEven(N) {
    if (N == 0) {
        return true;
    } else if (N == 1) {
        return false;
    } else if (N > 1) {
        return isEven((N - 2));
    } else if (N < 0) {
        return isEven((N + 2));
    }
}
console.log(isEven(50));
// -> True
console.log(isEven(75));
// -> False
console.log(isEven(-1));
// -> False
function countBs(A) {
    var bcount = 0;
    for(i = 0; i < A.length; i++){
        if(A[i] == "B"){
            bcount++;
        }
    }
    return bcount;
}
console.log(countBs("BBC"));
// -> 2
function countChar(B,C) {
    var charcount = 0;
    for(i = 0; i < B.length; i++){
        if(B[i] == C){
            charcount++;
        }
    }
    return charcount;
}
console.log(countChar("kakkerlak", "k"));
// -> 4
function countBs(A) {
    return countChar(A,"B");
}
console.log(countBs("BBC"));
// -> 2
1 Like

Tougher exercises which took me a bit of time to work out. I failed to solve for countBs on my own and had to look at the solution. I think I just misunderstood the instructions:

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

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

Bean Counting:
failed.

Keep up the great work. Keep trying and you will eventually get a hang of it. There are plenty of answers here on the forum that you can take inspiration from.

Happy Learning! :smile:

  1. Minimum
    function min(a,b){
    if(a < b){
    return a;
    }
    else{
    return b;
    }
    }
    console.log(min(0, 10));
    // → 0
    console.log(min(0, -10));
    // → -10

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

*3. Bean Counting - *This kicked my butt haha. I didn’t solve this by myself but did my best to work step by step, line by line through it trying to understand why. I wrote notes throughout this problem trying to simplify it, hoping that breaking down the problem would help. In some ways it did but I just feel like I need more practice.
//Make Function countBs
//Make Loop
//Use string.length
//Make Function countChar
function countChar(string,x){
let counter = 0;
for (let i = 0; i < string.length; i++){
if(string[i] == x){
counter += 1;
}
}
return counter;
}
function countBs(string){
return countChar(string,“B”);
}

1 Like

Thank you, Malik. I appreciate your encouragement. You are awesome, Sir :slight_smile:

1 Like

no problem. Booleans return a true or false. The quotations aren’t necessary.

  1. function smaller(x,y){
    return Math.min(x,y);
    }
    document.write(smaller(1,100));

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

}

document.write(isEven(50));
document.write(isEven(75));
document.write(isEven(-1));

var x=prompt(“I will tell you how many B has your word”)
function countBs(x) {
var count = 0;
for(y = 0; y < x.length; y++){
if(x[y] == “B”){
count++;
}
}
return count;
}
document.write(countBs(x));

var x=prompt(“I will tell you how many your choosen characters has this word:”)
var z=prompt(“choose your letter”)
function countBs(x,z) {
var count = 0;
for(y = 0; y < x.length; y++){
if(x[y] == z){
count++;
}
}
return count;
}
document.write(countBs(x,z));

1 Like

Min Exercise

function min(param1, param2) {

if (param2 === undefined){
  return param1;
} 

if (param1 < param2) {
return param1;
}
else {
  return param2;
}

console.log(min(0, 50));
console.log(min(50, 7000));

Recursion

function isEven (param) {

if (param < 0) {

param = Math.abs(param);
}

 
if (param === 0) {
return true;
}
  
if (param === 1) {
return false;  
}

return isEven (param - 2);
}

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

Bean Counting

function countBs(string) {
  let result = 0;
  
  for (let i = 0; i < string.length; ii+) {
       
  if (string[i] === 'B') {
    
    result = result + 1;

  }
  }

return result;

}

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

  }
  }

return result;
}



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

1 Like