Chapter 3 Exercises

Minimum Calculation

var input1 = prompt("Enter the first number:");
var input2 = prompt("Enter the second number:");
function min(x,y){
  x = Number(x);
  y = Number(y);
  if (x<y){
    return x;
  }
  else if (y<x){
    return y;
  }
  else if (x===y){
    return x;
  }
  else {
    return null;
  }
}

document.write(“

The minimum of the two numbers is: “,min(input1,input2),”

”)

Recursive odd/even

var input1 = prompt("Enter the integer to test for odd/even:");
function odd_even(x){
  x = parseInt(x);
  if (x < 0){
    x = Math.abs(x);
  }
  if(x===0){
    return "even";
  }
  else if(x===1){
    return "odd";
  }
  else {
    x = x - 2;
  }
  return odd_even(x);
}
document.write("<h3>The number ",input1," is: ",odd_even(input1),"</h3>");

Bean counting

var input1 = prompt("Enter the string to consider:");
var input2 = prompt("Enter the character to count:");
function count_char(str,char){
  var count = 0;
  for(var i = 0;i<str.length;i++){
    //console.log("i = ",i, "char =",str[i]);
    if (str[i]==char) count++;
    }
  return count;
}
document.write("<h3>The number of ",input2,"'s"," in '",input1,"' is: ",count_char(input1,input2),"</h3>");
1 Like

Chapter 3 Exercises

minimum.js
  if(xxx <= yyy){
    console.log(xxx);
  }
  else if(xxx > yyy){
    console.log(yyy);
  }
}

findMinimum(2, 1);

recursion.js
  var input = xxx;
  while(xxx>=2){
    xxx -= 2;
  }
  if(xxx == 0){
    console.log(input + " is even.")
  }
  if(xxx == 1){
    console.log(input + " is odd.")
  }
  if(xxx < 0){
    console.log(input + " is not a natural number!")
  }
}
isEven(15);
isEven(3);
isEven(2);
isEven(1);
isEven(0);
isEven(-1);

bean_counting.js
  var counter = 0;
  var noOfBs = 0;
  var wordLength = fff.length;
  for(counter; counter<wordLength; counter++){
    if(fff[counter] == "B"){
      noOfBs++;
    }
  }
  var toReturn = "B";
  if(noOfBs == 0){
    toReturn += "s";
  }
  else if(noOfBs > 1){
    toReturn += "s";
  }
  console.log(fff + " has " + noOfBs + " " + toReturn)
}
countBs("hello");

function countChar(fff, xxx){
  if(xxx.length == 1){
    var inputChar = xxx;
    var counter = 0;
    var noOfChars = 0;
    var wordLength = fff.length;
    for(counter; counter<wordLength; counter++){
      if(fff[counter] == inputChar){
        noOfChars++;
      }
    }
    if(noOfChars == 0){
      inputChar += "s";
    }
    else if(noOfChars > 1){
      inputChar += "s";
    }
    console.log(fff + " has " + noOfChars + " " + inputChar)
  }
  else{
    console.log(xxx + " must be 1 character!");
  }

}
countChar("hello", "l");
1 Like

I am chapped that it works! First code I did 100% solo.
var a = prompt(“pick a number”);
console.log(“you picked”, + a);
var b = prompt(“pick a different number”);
console.log(“you picked”, + b);
function min (a,b){
}
console.log(“the min is”, + Math.min(a,b));

2 Likes

// Minimum:

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

// Recursion:

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

}
console.log(isEven(10));
console.log(isEven(80));
console.log(isEven(-15));

// Bean Counting: (I had to look online for this one…!)

function countBs(str) {
var count = 0;
for (var i = 0; i < str.length; i++) {
if (str.charAt(i) === “B”) {
count++;
}
}
return (count); // return outside of for loop
}

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

function countChar(str, char) {
var count = 0;
for (var i = 0; i < str.length; i++) {
if (str.charAt(i) === char) { // use the variable char instead of the string “char”
count++;
}
}
return (count); // return outside of the for loop
}
console.log(countChar(“kakkerlak”, “k”));
document.write(“B comes in BCC >> " + countBs(‘BBC’) + " times
”)

@Cryptoscoot
Hi Sir, IsEven function is not correct. you should subtract n by 2 (n-2). :slight_smile:

Happy learning,
Abel S

1 Like

Thank you, I was playing around with it after I got it to work to see how it all worked and I missed making that change back to the original code.

Sorry about that.

1 Like

Chapter 3 Excercizes

  1. Minimum

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

  2. Recursion

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

  3. Bean Counting

1 Like

Chapter 3 Excercises

  1. Minimum unction min(a,b) {
    if (a<b){
        return a;
    }  else {
       return b;
    }
    console.log(min(10,0));
    }
    
  2. Recursive
    function isEven(n) {
      if (num == 0) return true;
      else if (num == 1) return false;
      else if (num < 0) return isEven(-n);
      else return isEven(n - 2);
    }
    
  3. Bean Counting
    var word=prompt("What is your word?")   
    function countX(word) {
    let bs=0;
    for (let x=0; x<word.length; x++){
    
    if (word[x]=="B"){
        bs+=1;
    }
    }
    return bs;
    
    }
    function countBs(word){
        return countX(word);
    }
    alert(countBs(word));
    

    PART 2 Choose character

    var word=prompt("What is your word?")
    var char=prompt("What is your word?")   
    
    function countX(word,char) {
    let bs=0;
    for (let x=0; x<word.length; x++){
    
    if (word[x]==char){
        bs+=1;
    }
    }
    return bs;
    
    }
    function countBs(word,char){
        return countX(word,char);
    }
    alert(countBs(word,char));
    
1 Like

min exercise

var min = function(a,b){
return Math.min(a,b);
}
console.log(min(2,4));

i wrote the above code and it worked fine when inputting different arguments. however, it differs from the solution in the book. is there a disadvantage to my code? perhaps the fact that I created a global variable “min”? the exercise in the book mentions the Math.min function so i figured it was neccessary to use that in the solution.

1 Like

Hi @Regis_Brickett,

Over here, you are directly using the function defined by the Math object which is inbuilt into Javascript with it’s own logic. The question wants you to answer with your own logic without using any inbuilt functions.

Both of the answers are right but one of them uses an inbuilt function, the other is custom made by yourself.

Hope this clears it out for you.

  1. Math.min is technically a function (static function), however this exercise called for the recreation of a function that produces the same value (result).

function Example-
function returnMin( a , b ) {
if ( a < b ) {
return a;
} else
return b;
}

returnMin();

Array Example-
const array = [5,3,1,11];

console.log(Math.min(…array));

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

  1. Bean counter// Admittedly I had to copy and learn along the way for this one…

function countBs(str){
var count = 0;
for ( var i =0; i < str.length; i++) {
if (str.charAt(i)===‘B’){
count++;
}
}
return (count); //return outside og the loop.
}

document.write(countBs(fBBBbBBBbaaa’));


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

alert(countChar(‘hhahhhaa’, ‘h’));

1 Like

My solution to the Even or Odd excercise. I added some console.logs on every subloop as I needed to debug at some point:

  let inputNum = parseInt(prompt("Give me a number"));

  function isEven(original,number) {
    if (number == 0) {
    console.log("ZERO");
    return(original + " is even");
    } else if (number == 1) {
    console.log("ONE");
    return(original + " is odd");
    } else {
    console.log("RECURSE");
  return(isEven(original,number-2));
  }
  }
  console.log(isEven(inputNum,inputNum));

My solution to the counting Letters Excercise. It takes user input for the word to be checked then user input dor the letter to be counted:

  let word = prompt("Type in a word");
  let inputChar = prompt("type in a letter");



  function countBs(word,inputChar) {
    let counter = 0;
    let totalFound = 0;


    while (counter < word.length) {
      if (word[counter] == inputChar) {
      console.log("LETTER FOUND");
      counter++;
      totalFound++;
    } else {
      console.log("LETTER CHECKED");
      counter++;
  }
}
return(totalFound);
}
console.log(countBs(word,inputChar));
1 Like

Minimum Exercise:
function min(x,y){
if(x < y){
return x
} else {
return y
}
}
console.log(min(0,10))
console.log(min(0,-10))

Exercise Recursion:
function isEven(number){
if(number == 0){
return true
} else
if(number == 1){
return false
} else{
return isEven(number - 2)
}
}
console.log(isEven(50))
console.log(isEven(75))
console.log(isEven(-1))
Due to -1 being lower then 0, n-2 recursion will never reach 0 or 1 to return a boolean but will start an infinite loop. to avoid this we would have to add a step that gets the absolute value of that number and continue the program.
function isEven(number){
if(number<0){
number = Math.abs(number)
}
if(number == 0){
return true
} else
if(number == 1){
return false
} else{
return isEven(number - 2)
}
}
console.log(isEven(-1));

Exercise Bean counting:
function countBs(word){
let totalBs = 0
for( let i = 0; i < word.length; i++){
if(word[i] === “B”){
totalBs++
}}
return totalBs};

To make a second parameter, all that needs to be done is adding a parameter named letter and replace all “B” with the new parameter so you are able to expand the program to find the amount of any letter in a string

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

1 Like

I am having a hard time understanding the meaning of “For any other number N, its evenness is the same as N - 2” If we have a “else return isEven(n - 2)”, I see that it works to print out the right boolean but WHY? Can anyone help me understand?? Everything else in the solution makes sense.

MINIMUM;
function minimo(a, b){
if (a<b)return a;
else return b;
}
console.log(minimo(2,5))

2.-RECURSION:
var number = prompt(“Number please:”);
var msj = isEven(number);
document.write(msj);
function isEven(x) {
if (x == 0) {
return (“Even”);
}else if (x == 1) {
return (“Odd”);
}else if (x < 0) {
return isEven(-x);
}else {
return isEven(x - 2);
}
}
3.-BEAN COUNTING
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;
}

i couldnt finish by myself this one so i checked the community and see other examples , i hope to understand better more about how to code it right during the course

A)
let a=(123-321);
undefined
var b= (223-354);
undefined
minnum=(a,b);
-131

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

function min(i,j) {
if (i <j) {
return i;
} else if (j < i) {
return j;
} else {
document.write;
return j;
}
}

function b/counter(word, letter){
var b = 0
for(var count = 0; count < word.length; count++){

if (word.charAt(count) == letter)
b++

}
console.log(b)
}

var numA = prompt("Pick a number: ", 0);
var numB = prompt("Pick a number: ", 0);
if(numA<numB){
console.log(numA);
}
if(numA>numB){
console.log(numB);
}

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

Coming soon. Have to work in the morning!

Minimum:

function min (value1, value2) {
if (value1 > value2) { return (value2); }
else { return (value1);}}

Recursion:

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

Bean Counting;

function countChar(line, letter) {
let count = 0;
for (let c = 0; c < line.length; c++) {
if (line[c] == letter) {
count += 1;
}
}
return count;
}

function countBs(line) {
return countChar(line, “B”);
}-

cont.,

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

}

console.log(countBs(“BBC”)) ;
console.log(countChar(“kakkkerlakk”, “k”)) ;

okay here are my exercises, they suck but sometimes i cant figure out what the writer is asking us to do, he explains weird :sweat:

EXERCISE 1

function min(a, b){

if(a>b){console.log(b + " is the smallest number");}

else{console.log(a + " is the smallest number");}

}

EXERCISE 2

function isEven (a){

if(a%2==0){return true;}

else{return false;}

}

EXERCISE 3

couldnt do it myself :(, had to copy ivans exercise :((((

function countChar(string, D){

let count = 0;

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

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

return count;}

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

1 Like