Ryan Evans
Flatiron School Blog


Ruby | Rails | SQLite | React | Redux | JavaScript | HTML | CSS

Case vs. Hash

I was working on a text editor code challenge in Ruby recently. The solution had to accept input that included an operation number. Each operation number represented a method that would execute the corresponding operation. For example, operation 1 should call the append() method, which adds characters to a string. Operation 2 should call the delete() method that removes characters from the string, and so on.

A common way to solve this, is to create a case statement. It can also be done with an if/else statement, but the problem with an if/else statement is that if you were to add more operations, it’ll get cumbersome quickly.

Let’s take a look at what the case statement might look like:

case @operation_number {
  when 1
   append(characters)
  when 2
   delete(characters)
  else
   raise 'Please enter a valid operation number'
  end
}

Nested For Loops and Big O

When comparing two strings or arrays, we tend to take the easy route which is to nest for loops, comparing each element from the first object to all the elements of the second object in a nested loop.

The problem with this approach, is that the time complexity is O(n^2). So, as your input grows, the runtime required gets significantly larger.

So, how can we still accomplish the comparison, but with a more reasonable, smaller time complexity?


Let’s look at an example. Let’s determine if two strings are anagrams. Remember what an anagram is? Here’s the definition from Wikipedia:

An anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.[1] For example, the word anagram can be rearranged into nag a ram, or the word binary into brainy or the word adobe into abode.


Here’s an approach to comparing two strings that doesn’t utilize nested for loops. Instead, we’ll create two separate for loops.

We’ll use the first loop to add each character of the first string (str1) to an object (obj1). The character (char) will be the key, and we’ll set it’s value to 1. If a character is already in the object, we’ll increment it’s value by 1.

Now let’s move on to the second loop. This time, we’ll check the object (which holds the counts of each character from string1) to see if each character from string2 (str2) is there. If it isn’t, then we can return false, because both strings must have the same characters in order to be an anagram.
If the character is there, we’ll decrement it’s value by 1. By the end of the loop through string2, if all characters have been found and decremented in the object, we can return true… right?

Well, almost. We can if, before we begin the character comparisons, we verify that the lengths of both the strings are the same. This way, we won’t miss any extra characters that don’t get caught by comparing characters in string2 to those in object1.

Take a look at the example code:

``` function isAnagram(str1, str2){


Rest Parameters in JavaScript

If you’re writing ES6 compatible code, using rest parameters is preferred over using the arguments object (MDN). If you remember, the arguments object represents all the arguments passed to a function that accepts an unspecified number of arguments. It is like an Array in that it has a length property and indexes, but doesn’t have the same built-in methods as an array.

Here’s an example (from MDN) of how you would use arguments with a function:
``` function func1(a, b, c) { console.log(arguments[0]); // expected output: 1


Update Your Copyright Year Automatically

Happy New Year!! 🎊

Did you remember to update your websites with the new copyright year?

Here’s how to set it up with javascript to auto-update every year, so you don’t have to worry about it…

All you need to do is make sure that your copyright statement is within <p></p> tags so this will be visible (not just within <footer> tags.
Then, you can add the javascript code inline within <script></script> tags.

One important thing to note first… if you didn’t already know, the getYear() method is deprecated, so you’ll need to use the getFullYear() method.


Here’s an example of what the code looks like:

<footer>
   <p>website designed and built by Ryan Evans &copy;
      <script language="JavaScript" type="text/javascript">
         var today = new Date();
         var year = today.getFullYear();
         document.write(year);
      </script>
   </p>
</footer>



And there you have it! Easy enough, right? Now if you add this little snippet to your sites, you won’t have to worry about updates every year! That <script> will display the current year right next to the &copy; copyright symbol.

Here’s wishing everyone a happy, healthy and prosperous new year!


Date.prototype.getFullYear() on MDN


Big O Notation

In a recent post, I mentioned Big O notation:

Simply put, Big O notation is used to describe the running time or space requirements of executing an algorithm as the size of the input to the algorithm increases. The differences between two functions that do the same thing, but in different ways (like iteration vs. recursion), becomes very clear when you consider very large datasets passed into the algorithm.
Big O focuses on the worst-case requirements of running an algorithm, which can be referred to as the upper-bound of the growth rate.

I thought I should expand on this and share some good resources for learning more about Big O. It can be a bit confusing to understand at first. I’ve found that different sites explain it differently, using different examples. So some examples might make more sense to some people, which can help immensely when learning something like Big O.


For the record, here’s the definition of Big O from Wikipedia:

Big O notation is a mathematical notation that describes the limiting behavior of a function when the argument tends towards a particular value or infinity. In computer science, Big O notation is used to classify algorithms according to how their running time or space requirements grow as the input size grows.

Big O notation characterizes functions according to their growth rates: different functions with the same growth rate may be represented using the same O notation.

The letter O is used because the growth rate of a function is also referred to as the order of the function. A description of a function in terms of big O notation usually only provides an upper bound on the growth rate of the function.


The main point here is, what are the worst-case requirements (in terms of space or time) that will be needed to run an algorithm as the input gets huge. If you remember to think in terms of huge amounts of data being fed to your algorithm to process, you’ll understand why things like smaller exponents, multipliers and additions are simply ignored (because they become irrelevant as the data gets larger, compared to the lead exponent).


Take a look at some of these references to find one that connects for you: