React.js TDD

In this TDD case we will use React.js and when I was working in this project I found that is very important to have the right version of dependencies installed, or it will not work, and many other developers were reporting the same problems for that reason I will start providing the “package.json” file with the versions of the dependencies that will make your testing experience, much better.

Now that we have the right set up lets start creating the file tree, and here is a picture of the one that I use in this case.

Here is the test code:

Now run the test with the Terminal command: sudo npm run test

And it fail, that is what it should happen this is TDD. 1st fail then Succeed, and now here is the code for this code to pass, the following code will test if the file “App.js” renders and will create a snapshot of the file.

The following is a picture of what you should see as feed-back that the test has pass.

and there you have it, this is the basic code template for React.js TDD from here create more components, test and create using: React.js + TDD.

Category: TDD | Comments Off on React.js TDD

Quick Node.js-Express Serving HTML Files

Ok. what if you have a Node.js-Express Server, sure you can use Jade or EJS using the template engine but how do you serve the most basic “index.html” files. the following is the code for a bare-bones quick server for “.html” files.

Here is a picture with the file-tree

The HTML file are in the Directory “pages”.

code in “package.json”:

 

For better results on development use: NODEMON

Category: Node | Comments Off on Quick Node.js-Express Serving HTML Files

Recursive Functions

Recursive functions call themselves
Make sure to write the base exit & Modify the input counter or you will shoot for infinity.

To read more about recursion here is a good blog post: LINK

Here is an example:

This function will print:
5
4
3
2
1

An that is a basic intro to recursion.

Now I read an article that says that you can create the Fibonacci sequence using Recursion.

Example of the Fibonacci sequence:
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]

and here is my code to generate recursive Fibonacci:

and here is the run time:

 

Latter I found a more compact solution in the internet:

This little code might seem better but is not, this has an exponential run time, my solution that has more code, actually has a much much better execution time.

here is the run-time for the same tests using the short code (Note I had to delete the call for fib 50 because would just take forever.):


Here are a couple of basic functions that use recursion:


Here is my take on the recursive solution to the Merge-Sort algorithm:

If you are new to recursive functions this might not make sense, but the real importance goes in the line with the code: return joinarr( myMerge(h1), myMerge(h2) )

Here is the complete code:

.

 


Category: JS | Comments Off on Recursive Functions