Pages

Monday, August 29, 2016

JS exercises - Palindrome

Write a recursive JS function to determine if a string is palindrome or not.

Return true if it is a palindrome, and false if it is not a palindrome.

I used regex to first get rid of the spaces and any puntuation.

For example :

Madam, I'm Adam - should return true, because it's the same backwards as forwards.

Answer in the comments.

JS recursive exercise

This one feels like it's going to be very easy.

Write a recursive program to determine whether a number is even or odd.

What is wrong with just saying num % 2 === 0     ?

Yeah, I don't know why you would spend time doing this but:


function is_even_recursion(number){
  if (number < 0) {
    number = Math.abs(number);
  }
  if (number === 0) {
    return true;
  }
  if (number === 1) {
    return false;
  }
  else {
    number = number - 2;
    return is_even_recursion(number);
  }
}

It works!

JS recursive function exercise

If I hear the word fibonacci again, I'm going to vomit.   So, I'll skip the fib question from the website that I'm stealing all these problems from:  http://www.w3resource.com/javascript-exercises/javascript-recursion-functions-exercises.php

I'm sorry, I am just so sick of Fibs and freaking fizz-buzz.  Are there no other examples out there?

Well, maybe there just is a reason, so let's do the Fib problem after all.  Just deal with it:


Write a recursive program to get the first n Fib numbers.

For example, fib(7) would output [1,1,2,3,5,8,13];

However, this is worth doing and seeing the answer in the comments, because this is probably the smartest solution to this problem that I've ever seen.




Recursive reflections

If I call an inner function recursively, will it remember the setting of variables in an outer function?

The answer is yes!

For example:

var tester = function(a){
b = 0;
function poop(){
if(a === b){
return a;
}
console.log(b);
b++;
poop();

}
poop();
}

var maple = tester(5);
0
1
2
3
4

will print to the console.  I suppose this is a recursive function that produces only side effects and returns nothing.

JS recursive exercise 5

Write a recursive program to compute the exponent of a number:

8^3 = 8 * 8 * 8 = 512

This one is kind of easy, if you've finished the others.

You're just multiplying, 8 by all the other eights and returning the number.

Don't make this harder than it needs to be.

Once you've multiplied one eight, you've got one less exponent, etc...

JS recursive exercise 4

Write a recursive program to sum up an array.

Easily done with reduce, but more fun with recursion:

var sumMeUp = [3,4,5,6];

output should equal 18

Hint:  Do you want a hint?  If so, read down below:

pop it + everything else in the array.

Just keep popping.

Recursion exercise 3

By the way, I stole these problems from : http://www.w3resource.com/javascript-exercises/javascript-recursion-functions-exercises.php


Write a recursive function to get integers in a range.

For example, range(2,7)    will return [3,4,5,6];

Yes, that is returned in an array.

Answer is in the comments.

Recursion exercise 2

Write a recursive JS program to find the greatest common divisor of two numbers.

Do you have no idea what the greatest common divisor is?  Khan Academy video

This problem is really easy, if you understand the Math part of the problem very well.

If the lower number of the two can evenly divide into the larger number, then that is the greatest common divisor.  Just return that number.

Else, what's the remainder from dividing the larger number by the smaller number?

Will that number divide evenly into the smaller number?

No?

Well, then what's the remainder from dividing the smaller number by that number?

Will that remainder divide equally into the smaller number?  If yes, you've found your answer.

For example,


36, 96 - What's the greatest common divisor?


Step 1 - will 36 go into 96 evenly?  No.  Remainder = 24.

Step 2 - will 24 go into 36 evenly?  No.  Remainder = 12.

Step 3 - will 12 go into 36 evenly.  Yes. Answer = 12.

12 is the greatest common divisor of 36 and 96.


Answer is in the comments.

Recursion exercises 1

Write a recursive JS program to calculate the factorial of a number.

For example. 5! = 5 * 4 * 3 * 2 * 1 = 120
   

One easy way to set this up is to create a base case that returns 1, once the decremented number reaches 1.  By decremented number, I mean the recursive call the number being fed is decremented each time until 1 is reached.  Then all the numbers come back and multiply themselves together.


Since this first one is easy, I'm going to walk through it, incase you're unfamiliar with recursion :


var factorial = function(num){
if(num === 1){
return 1;
}
return num * factorial(num -1);
}

var maple = factorial(9);

maple will equal 362880


First, the factorial function takes 9 as an argument, checks to see if it is equal to 1, it's not, it returns the value of 9 multiplied by the value received from calling the function factorial on 8.  This function is just calling itself rather than another function.  That value is then calculated by feeding 8 to another instance of factorial, it checks to see if 8 is equal to 1, it's not, so it returns the value of 8 multiplied by the value of calling factorial and feeding it 7.

This goes on and on until 1 is reached.  At this point the function returns 1 as the value.  That means that the function that had multiplied 2 by the value of factorial being fed 1, now has received the value from factorial being fed 1, that value is 1.  So 2 will be multiplied by 1, and that equals 2, and that will be returned to the function before it, that had called factorial and fed it a 3 and had returned the value 3 multiplied by the value of factorial fed 2.  Well, that factorial fed 2 now has a value, and that value is 2.  So, 3 times 2 is six, and this value gets returned to the function that is waiting for that value, and so on, and so on, until we reach the very first function that started all this.

It may take a while to wrap one's head around.


Thursday, August 18, 2016

Sorry to throw this in the middle, but how the heck do you use bind? What is bind?

var dog = {
a:5,
b:10,
c:11
}

var cat = {
a:50,
b:100,
c:110
}

var fish = {
a:500,
b:1000,
c:1100
}

var bird = {
a:5000,
b:10000,
c:11000
}

var mammoth = {
a:50000,
b:100000,
c:110000
}

var howMany = function(){
console.log(this.a);
}

var howManydogs = howMany.bind(dog);
var howManyCats = howMany.bind(cat);
var howManyFish = howMany.bind(fish);
var howManybirds = howMany.bind(bird);
var howManyMammoths = howMany.bind(mammoth);

howManyMammoths();   will equal 50000
howManydogs();              will equal 5


And that's all there is to it.  Bind simply gives a context to 'this'.

Saturday, August 13, 2016

Cool javascript functions part 2

swaparoo(a, b){
  var temp = a;
  a = b;
  b = temp;
}

x = 1;
y = 2;
swaparoo(x,y);


x = ?


Answer is in the comments!

Cool javascript functions part 1

function happy(a){
  a = null;
}

x = [];
happy(x);
x = ?

What will x equal?

The answer is in the comments.