Chapter 3 Exercises

Math.Min:

var x = 7, y = -4;
var z = Math.min(x, y);

console.log(Math.min(x, y));

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

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

Chapter 3 Exercises
Minimum-

  function min(x,y){
    if (x<y){
      return ("Of " + x + " and " + y + ", " + x + " is lower");}
    else if(y<x){
      return ("Of " + x + " and " + y + ", " + y + " is lower");}
    else {
      return (x + " and " + y + " are the same number")
    }}

Recursion-

  function isEven(x){
    if (x%2==0){
      return (x+" looks even to me!</br>");}
    else {
      return (x + " looks pretty odd if you know what I mean.</br>")
    }}

BeanCounting-

  function countBs(str){  // is 'str' just a placeholder then?
    var count=0;
  for(var a=0;a<str.length;a++){
    if (str[a] === "B"){
      count+=1;}}
    return (count);}

document.write(countBs("Booya"));
function countChar(str, ltr){
  var count=0;
for(var a=0;a<str.length;a++){
  if (str[a] === ltr){
    count+=1;}}
  return (count);}

document.write(countChar("Booya","o")+"</br>");
1 Like

Booya Chakka!!
nice!
Ivo

1 Like

Screenshot 2020-04-14 at 9.04.23 PM

Screenshot 2020-04-16 at 1.40.36 PM

Screenshot 2020-04-16 at 2.16.14 PM

1 Like

It feels good writing something that works!

1 Like

Interesting recursion solution. I tinkered a little with it to try and understand how it works.
It doesn’t seem like the -
else {
isEven(-N);
-bit is needed for the code to run correctly since it’s already explained in the initial if-statement. Still trying to understand that initial if-statement though.

Here’s what I think it means “If N is not zero and not 1 and N is bigger than 2: execute N -2”
That would turn -1 into 1, and 50 into 48 and 75 into 73.
I think I’m leveling up :slight_smile:

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

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

Recursion
This one gave me a little bit of trouble, as for subtracting from subtracting because I started to question my old dusty high school math.
My conclusion is that when you put in a negative value the return isEven( - n) turn the value to zero since for example

  • 1 - 1 = 1 = false
    -2 - 2 = 2 =true

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

CountBs
Had a lot of trouble with this one. I now understand that this first half is a way to write a function for how to search through a word looking for a specific letter. The second half of the assignment I’m still not sure if I’ve understood correctly. It seems to me that the only thing I’ve managed to achieve is to move the execution of what letter to look for from the function to the console.log. I’ve looked at more than thirty different student solutions to this by now and by trial and error am going to assume that this was the purpose of the exercise and that I just don’t fully understand why I would like to move the execution like that yet.

function countBs(word){
let count = 0;
for(i = 0; i < word.length; i++){
if (word[i] == “B”) {
count++;
}
}
return count;
}
function countChar(word, char){
counter = 0;
for(i = 0; i < word.length; i++){
if (word[i] == char) {
counter++;
}
}
return counter;
}
console.log(countBs(“BilligtBaByBengt”));
console.log(countChar(“Rapakaljaaa”, “a”));

remember to use preformatted text when you post your answers.


Ivo

1 Like

//Min function

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

//Iseven problem

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

//Counting Beans

I didn’t know how to do this exercise so I’ve check the internet and I found this answers, that sincerely I don’t understant at all. Moreover, the second part of the exercise gives some kind of error.
Anyway, now checking the colleagues answers, I see things that I didn’t know as well like this “[i]” to refer or point a characther. I could’ve guess i supposed.
I know is not the right solution, but it is a solution. Keep calm and carry on! :slight_smile:

function countBs (x){
console.log((x.match(/B/g) || []).length);
}
countBs('Batman is not a Bad bboy')

function countAnyCh (x, y){
console.log(x.match(new RegExp( y, 'g')) || []).length;
}
countAnyCh ('Batman is not a Bad bboooy', 'o')

I don’t know why but when I copy-paste the solution when editing the post it works in the console. But when the post is published, it don’t… :cry:

Do the rest of the code with correct format, see my post below.
ivo

1 Like

The book is also a good reference and the sandbox for the book. to test your solutions. There is no need to invent the wheel again.:wink:
https://eloquentjavascript.net/2nd_edition/code/#3

1 Like

You have to use preformatted text to poost code. It’s not just a pretty function. Its crutial when you post code and want help. Please take your time to format the code with correct format. then it will work. :slightly_smiling_face:

please try it on your code now.


Ivo

1 Like
  1.    function min(y,z){
         if (y<z) return y;
         else return z;
       }
    
    
    

console.log (min(2,6));
console.log(min(-34,123))

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

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

3. `function countChar(string, ch) {
    let counted = 0;
    for (let y = 0; y < string.length; y++) {
      if (string[y] == ch) {
        counted += 1;
      }
    }
    return counted;
  }

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

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

Minimum…
function min(a,b) {

      return Math.min(a,b)
    }

      console.log (min(5,6));
      console.log (min(5,-5));

Recursion.
function isEven(x) {
if (x < 0) {
x = Math.abs(x);
}
if (x===0) {
return true;
}
if (x===1) {
return false;
}
return isEven (x - 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.lenght; i++) {
          if(string[i] === 'B') {
            result = result + 1;
          }
        }

    return result;
  }
    function countChar(string, character){
      let result = 0;

        for(let i = 0; i < string.lenght; i++) {
          if(string[i] === character) {
            result = result + 1;
          }
        }

    return result;

    }

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

Hey Guys,
Can someone tell me why i am getting the answer 0 on my Bean Counting excersise?
I have gone over it loads of times and cant see what the problem is!!
Thanks
Andy

1 Like

Hi, because you have misspelled length.
Try it again! :slightly_smiling_face:
Zoltan

1 Like

Minimum

Recursion

Can someone check if my
else if(a < 0){
return (isEven(a+2));}

solution is okay? Or should I have used the (-a) instead? @ivga80

Bean counting

I was struggling with this one. I think, I need to start this whole course from the beginning…

1 Like

Oh my god, How embarrassing, Thanks Zoltan!!

1 Like

MINIMUM

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

      else if (y < x) {
        return y;
      }
    }

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

BEAN COUNTING

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

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


console.log(countBs("BBBC"));
console.log(countChar("mississippi", "s"));
1 Like

Minimum

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

1 Like

Hi.
Sure it’s ok, If it works, it is passed! :v::partying_face:

I just wish you wouldn’t make it so time-consuming for us to test your codes. :pleading_face:
I don’t know if you are aware of it, but we have to rewrite your entire code letter for letter, line for line when you post a code you want us to review as a screenshot?
If you just use the preformatted text instead, we would be able to copy&paste it in seconds instead of the tiering job of typing everything into my own editor. :anguished:

I was struggling with this one. I think I need to start this whole course from the beginning…

Don’t do that, Zoltan. feel free to go back and practice etc, etc. That’s very smart and we all should practice much more. But please don’t “go back and start the course from the beginning” that will only demotivate you when you get back to this point again.

Because this is new information that we need to learn and it will not help you anything by going back to the safe things you already know… You have to push on through this fase.

If you want to spend more time on the topic you are struggling with, I’ll be glad to help you with other resources etc, etc. so you can get inputs from other places, just contact me. and I’ll do my best to help you through this shit. Ok, bro?

Ivo

2 Likes

Wow, Ivo,
Thanks a lot! Finally I can post code the right way!
I’ll ask you when I get totally stuck.

Thank you so much for your input. :pray: :pray: :pray: :slightly_smiling_face:
Zoltan

1 Like
  1. Minimum

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

  1. Recursion

function isEven (num) {

if (num< 0) return “Number must be positive”;

if (num === 0) return "Even";

if (num===1) return "Odd";

return (isEven( -1));

}

I can’t find an Editor to run the code to see if I get an answer. Anyone have a suggestion?

  1. Bean Counting-

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

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