In this post, I’ll compare linear search and binary search algorithms. You’ll see pseudocode for each algorithm, along with examples and a step-by-step guide to implementing each.
In this post, I’ll compare linear search and binary search algorithms. You’ll see pseudocode for each algorithm, along with examples and a step-by-step guide to implementing each.
Have you ever encountered a problem you felt could be solved with recursion, except you didn’t know where to start? Or did it seem like you had to hack your way to a solution? The first part of tackling recursion is understanding when a problem calls for it.
Previously, I wrote about using the state pattern to manage application state. We will build on this idea with finite state machines. A finite state machine is a way to model state transitions in a system.
What is state? An object’s state is its data, its properties. For a book object, the state could include the title, author, and ISBN code. For a customer object, the state could be a first name, last name, and address. There is another use of the word state that applies to an object’s condition.
If you need a better way to manipulate large lists in JavaScript, iterators and generators are an improvement to using loops. Learn all about them in this tutorial.
Some problems are more naturally solved using recursion. For example, a sequence like the Fibonacci sequence has a recursive definition. Each number in the sequence is the sum of the previous two numbers in the sequence. Problems that require you to build or traverse a tree-like data structure can also be solved with recursion. Training yourself to think recursively will give you a powerful skill to attack such problems.
Sets can be very powerful if you understand how to use them. A few applications of sets include solving counting problems where you may want to find the number of ways you can group elements together. Another application is problems that require you to find relationships between elements. The ECMAScript specification doesn’t include methods for operating on sets in this way. But it is possible to create your own.
In part one of this tutorial, we learned the principles behind asynchronous programming and using callbacks. To review, asynchronous programming allows us to write code that is non-blocking by executing blocking tasks later. Callback functions provide a way to synchronize the execution of our code. However, nesting callbacks repeatedly is not a good pattern to follow. Here come promises to the rescue.
There is a lot of talk about asynchronous programming, but what exactly is the big deal? The big deal is that we want our code to be non-blocking. Tasks that can block our application include making HTTP requests, querying a database, or opening up a file. Some languages, like Java, deal with this by creating multiple threads. However, JavaScript has only one thread, so we need to design our programs so that no task is blocking the flow.