Pages

Thursday, September 1, 2016

How to create your own snippet in Sublime Text 3

Tools => developer => new snippet


Delete everything between the first brackets after CDATA.

Fill in with what you want to populate on the page.

Uncomment tabtrigger line.  Change the word between the tabTrigger tags to be any word you want.  I'll refer to this as your special word.

Save file.

Give the file a name, and end the file name with  .sublime-snippet

Boom, you're done.  Open a new file.  Type your special word and press tab.  Boom!

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.