Chapter 3 Exercises

First

function min(a, b) {

var result =0;
if(a > b) result = b;
else result = a;
return result;

}
(I wrote a document write to print the result here, but later deleted it before this.
let c = min(-19,3);

2nd Task

var result;

function oddEven(x, b = -2) {
if (x < 0) x = x*-1;
else if(x == 0) return result = “even”;
else if(x == 1) return result = “odd”;
else x = (x + b)
return result = oddEven(x);
}

document.write(oddEven(-1))

Exercise 3 I for some reason had recursion in my head and got completely lost. Now I’m aware it’s not recursion, and after reading answers, there’s little point in posting what I have then written.

Whoops.

1 Like

My indentations do not seem to format correctly from Atom to here, but there were indentations.

I could only do the first one:

function min(a, b){
    
    console.log(Math.min(a, b));
                }
min (412, 2544);
// 412

The second and third exercises I couldn’t figure out! When I checked the answers there are a few lines in the code that wasn’t in my vocabulary :frowning:

I hope after finishing Javascript course I can go and do many many exercises to start coding more fluently

1 Like

MINIMUM


function returnMin(a,b){
if(a<b){
return a;
}
else return b;
}
console.log(returnMin(5,4));

Recursion


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

BEAN COUNTING

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

1 Like

1- Minimum code:
var a = 5;
var b = 10;

    function minimal() {
    alert  (Math.min(a,b)) ;
    }
   minimal() ;
1 Like
  1. Minimun

function min(x,y){
if (x<y) {console.log(x);}
else console.log(y);}
console.log(min(0,10));
console.log(min(0,-10));

  1. Recursion

var number= prompt(“number please”);
var n = parseInt(number);
function isEven(n){
if(n==0) console.log(“even”);
else if (n==1) console.log(“odd”);
else if (n<0) return isEven(-n);
else return isEven(n-2);}
console.log(isEven(n));

  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(“BuBble”, “B”));

1 Like

1.Min

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

  1. isEven

function isEven(number){
if (number%2 === 0){
console.log(‘It is even’)
}
else{
console.log(‘It is odd’)
}
}

  1. countBs

function countBs (word){
var Bs = 0
for(i = 0; i < word.length; i++){
if (word[i] === “B” ){
Bs+=1
}

}
console.log(Bs)
}

  1. countCharr

function countChar (word, letter){
var capitalLetters = 0
for(i = 0; i < word.length; i++){
if (word[i] === letter){
capitalLetters+=1
}

}
console.log(capitalLetters)
}

1 Like
  1. Minimum:
function min(a,b) {
        if(a<b) return a;
        else return b;
      }
      console.log(Math.min(23,19));
      console.log(Math.min(38,48));
  1. Recursion:
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);
    }
    console.log(isEven(50));
    console.log(isEven(75));
    console.log(isEven(-1));

I had initially started with the above but tried this instead, definitely makes more sense looking at the solution:

function isEven(x){
      if(x % 2 == Even){
        return true;
        }
        else(x == Odd){
          return false;
        }
        else if(x == x - 2){
          return true && false;
        }
      }
      console.log(isEven(50));
      console.log(isEven(75));
      console.log(isEven(-1));

3a. countBs:

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

3b. countChar:

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("FullTimeBlockchainByAugust"));
console.log(countChar("somesortofstring", "s"));
1 Like
  1. Function Min:
    function min(a, b) {
    if (a < b) return a;
    else return b;
    }
    console.log(min(0, 10));
    console.log(min(0, -10));

  2. Recursion:
    function isEven(num) {
    if (num == 0)
    return true;
    if (num == 1)
    return false;
    if (num < 0)
    return “??”;
    else return isEven(num - 2);
    }
    console.log(isEven(50));
    console.log(isEven(75));
    console.log(isEven(-1));

  3. Bean Counting:
    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(“BBC”));
console.log(countChar(“kakkerlak”, “k”));

1 Like

Some unexpected behaviour occured when I did this exercise:
30. String -array with months.
I got the requested number via prompt.
let reqMonth=prompt(“Which month”);

And then want to print the corresponding month:
console.log(“The month #%i is %s”, reqMonth, hw[reqMonth-1])
Console output:
The month #NaN is Feb

Why does reqMonth not convert to a number inside the log output, but it does convert to a number inside the array reference?

Best, Raphael

1 Like

Minimum
I added an extra functionality to check for use of equal values to prevent errors.

<script>
  function minimum(a,b){
    if(a<b){
      return a;
    }
    else if(a>b){
      return b;
    }
    else{
      return "a and b are equal";
    }
  }
  console.log(minimum(10,12));
  console.log(minimum(12,9));
  console.log(minimum(10,10));
</script>

Recursion version 1
if oddEven(x) is even return true
if oddEven(x) is uneven return false

<script>
  function oddEven(x){
    if(x==0){
      return true;
    }
    else if(x==1){
      return false;
    }
    else{
      return oddEven(x-2);
    }
  }
  console.log(oddEven(50));
  console.log(oddEven(75));
</script>

Recursion version 2
I added an extra else if statement to check for a negative variable x.
If x is negative it recursively calls itself bij adding 2 to x instead of subtracting.

<script>
  function oddEven(x){
    if(x==0){
      return true;
    }
    else if(x==1){
      return false;
    }
    else if(x<0){
      return oddEven(x+2);
    }
    else{
      return oddEven(x-2);
    }
  }
  console.log(oddEven(-1));
  console.log(oddEven(-2));
</script>

Bean counting

<script>
  function countChars(string){
    amount = 0;
    for(i=0;i<string.length;i++){
      if(string[i] == "B"){
        amount++
      }
    }
    return amount;
  }
  console.log(countChars("BABYBOOMER"))
</script>

Counting character of choice

<script>
  function countChars(string, char){
    amount = 0;
    for(i=0;i<string.length;i++){
      if(string[i] == char){
        amount++
      }
    }
    return amount;
  }
  console.log(countChars("alabama","a"))
</script>
2 Likes

var number = prompt(“Enter a number:”);
var a = isEven(number);
document.write(a);
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);
}
}

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

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

1 Like

Solution to Chapter 3 Exercise Minimum

function min(a, b) {

return Math.min(a, b);

}

console.log(min(779,77));

Solution to Chapter 3 Exercise Recursion

function isEven (number){

number = number - 1;

if (number == 0) {return evenOdd;}

else {

evenOdd = !evenOdd;

return isEven(number);

}

}

evenOdd = false;

console.log(isEven (3));

Solution to Chapter 3 Exercise Bean Counting (first part)

function countBs (myString) {

let NumBs = 0;

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

if (isItB (myString, i) == true) NumBs = NumBs + 1;

}

return NumBs;

function isItB (anyString, num) {

if (anyString[num] === “B”) {yesB = true;}

else {yesB = false;}

return yesB;

}

}

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

Solution to Chapter 3 Exercise Bean Counting (second part)

function countChar (charString, myChar) {

return countBs(charString, myChar);

}

function countBs (myString, character) {

let NumBs = 0;

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

if (isItB (myString, i, character) == true) NumBs = NumBs + 1;

}

return NumBs;

function isItB (anyString, num, char) {

if (anyString[num] === char) {yesB = true;}

else {yesB = false;}

return yesB;

}

}

console.log(countBs(“BbrBtBtyBqsssB”, “b”));

console.log(countChar(“BbrBtBtyBqsssB”, “B”));

1 Like

Minimum
function min (x,y)

{ if (x<y)

{ return x;}

else

{ return y;}

}

EVEN/ODD
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);}

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

1 Like

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

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, 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

1st Function

function min (num1,num2) {
if (num2 == undefined) {
return num1;
}
if (num1 < num2 ){
return num1;
}else{
return num2;
}

}
document.write(min(0,19));
document.write("
");
document.write(min(1,2,4,9));
document.write("
");
document.write(min(-22,0,8,99));

2nd Function

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

3rd function

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

}
return count;

}

console.log(countBs(“BBC”));
// → 2
console.log(countChar(“kakkerlak”, “k”));
// → 4

1 Like

1st function:

function min(x,y){
let output = “0”
if (x>y) output += y;
if (y>x) output += x;
console.log(output);
}

The other 2 I looked at the answers and followed along to get a better understanding

1 Like

Exercise 1 - Calculate Minimum

function fnCalcMin(c,d){
return Math.min(a,b);
}
document.write(“Smaller from 2 and 6 is >> " + fnCalcMin(2,6));
document.write(”
");
ocument.write("
");
document.write("Smaller from 10 and 3 is >> " + fnCalcMin(10,3));

Exercise 2: Recursion

function isEven(num) {
//convert to absolute value to account for negative numbers
num = Math.abs(num);
if (num === 0)
return true;
else if (num === 1)
return false;
else
return isEven(num - 2);
}
document.write("50 >> " + isEven(50) + “
”);
if (isEven(50) == true) {document.write("its even!

");} else {document.write("its odd!

");}
document.write("75 >> " + isEven(75) + “
”);
if (isEven(75) == true) {document.write("its even!

");} else {document.write("its odd!

“);}
document.write(”-1 >> " + isEven(-1) + “
”);
if (isEven(-1) == true) {document.write("its even!

");} else {document.write("its odd!

");}

Exercise 3: Bean Counting

function countBs(str) {
return str.match(/B/g).length;
}
function countChar(str, character) {
var matchExp = new RegExp(character, ‘g’);
return str.match(matchExp).length;
}
function countChar2(str, character) {
var count = 0;
for (var i = 0; i < str.length; i++) {
if (str[i] === character)
count++;
}
return count;
}
document.write(“B comes in BCC >> " + countBs(‘BBC’) + " times
”);
document.write("k comes in kakkerlak >> " + countChar(‘kakkerlak’, ‘k’) + " times
");
document.write("k comes in kakkekekkkkkkkekrlak >> " + countChar2(‘kakkekekkkkkkkekrlak’, ‘k’) + " times
");

2021-05-02T06:07:00Z

1 Like
function comparar(x,y){
    if (x>y){
      console.log( x + ' es mas grande que ' + y);
    }
    else{
      console.log( y + ' es mas grande que ' + x);
    }
    return;
}
comparar(5,3);

function parImpar(a){
resultado = a % 2;
console.log(resultado);
if (resultado == 0){
    console.log('El numero es par');
}
else{
    console.log('El numero es impar');
}
}
parImpar(6);
;

let cadena = 'aBcdBefBghiBjklBBBBjdjhshgs'
function contarB(x){
let contador = 0;
for (var conteo of x){
    if (conteo == 'B'){
        contador = contador + 1;
        console.log(contador);
       
    }
}
console.log(contador);
}

console.log(contarB(cadena));

function contarX(x,y){
let contador = 0;
for (var conteo of x){
    if (conteo == y){
        contador = contador + 1;
        console.log(contador);  
    }
}
console.log(contador);
}
console.log(contarX(cadena,'j'));
1 Like
<html>
  <head>
    <title>Exercice 3</title>
  </head>
  <body>
    <h1> Ivan on Tech JavaScript Programming for blockchain developers</h1>
    <h2>Exercices Chapter 3</h2>
    <h3>Minimum</h3>
    <p>The previous chapter introduced the standard function Math.min that returns its smallest argument. <br>
      We can build something like that now. Write a function min that takes two arguments and returns their minimum.
    </p>
    <script>
    const min = (a,b) => a < b ? a : b;
    console.log(min(9,2))
    console.log(min(1,2))
    console.log(min(-10,20))

    </script>
    <h3>Recursion</h3>
    <p>We’ve seen that % (the remainder operator) can be used to test whether a number <br>
       is even or odd by using % 2 to see whether it’s divisible by two.
       Here’s another way to define whether a positive whole number is even or odd:
       <ul>
         <li>Zero is even</li>
         <li>One is odd</li>
         <li>For any other number N, its evenness is the same as N - 2.</li>
       </ul>
        Define a recursive function isEven corresponding to this description. The <br>
        function should accept a single parameter (a positive, whole number) and return <br>
        a Boolean. <br>
        Test it on 50 and 75. See how it behaves on -1. Why? Can you think of a <br>
        way to fix this?
     </p>
  <script>
    const isEven = function(N) {
      if(N == 0) return true;
      if(N == 1) return false;
      if(N < 0) return isEven(N+2);
      return isEven(N-2)
    }
    console.log(isEven(50));
    console.log(isEven(75));
    console.log(isEven(-2));

  </script>
  <h3>Bean counting</h3>
  <p>You can get the Nth character, or letter, from a string by writing "string"[N].<br>
    The returned value will be a string containing only one character (for example,<br>
    "b"). The first character has position 0, which causes the last one to be found at <br>
    position string.length - 1. In other words, a two-character string has length <br>
    2, and its characters have positions 0 and 1. <br>
    Write a function countBs that takes a string as its only argument and returns <br>
    a number that indicates how many uppercase “B” characters there are in the <br>
    string. <br>
    Next, write a function called countChar that behaves like countBs, except <br>
    it takes a second argument that indicates the character that is to be counted <br>
    (rather than counting only uppercase “B” characters). Rewrite countBs to <br>
    make use of this new function.
<script>
const countBs = function(str) {
  return countChar(str, 'B');
}
const countChar = function(str, char) {
  let counter = 0;
  for(var i = 0; i < str.length; i++)
    if(str[i] == char) counter++;
  return counter;
}

console.log(countBs("BOB MARLEY"));
console.log(countChar("BOB MARLEY", 'Y'));
</script>
  </body>
</html>

1 Like