FCC Intermediate Algorithm Scripting Challenge – ‘Boo who’

This post will cover freeCodeCamp’s intermediate algorithm scripting challenge ‘Boo who’.

‘Boo who’ challenges us to write a function that checks if a given argument is classified as a boolean primitive.  If so, return true, if it’s anything else, return false.

The solution, once written, is quite simple but there are a few key concepts we have to understand before we can get there.

Here’s what FCC gives us as a starting point:

Now, we have to figure out…  What the hell is a boolean primitive?

First, let’s consider what boolean means?  Boolean is a word used in both math and computer science.  In computer science, it generally refers to a data type or a variable that can have only one of two values, usually true or false.

Boolean variables are useful in controlling the logic of an algorithm and are generally used in conditional statements such:

Second, what does primitive mean?  Mozilla, as always, gives a pretty good description of a primitive.  Basically, Mozilla tells us a primitive value ‘is data that is not an object and has no methods.’

Tangent on Primitive Types vs. Reference Type

According to the latest ECMAScript standard, JavaScript has 7 data types. 6 primitive types which are string, number, boolean, null, undefined and symbol and one reference type – object.

The difference between the two types becomes confusing when you consider that JavaScript provides constructor functions that allow you to wrap boolean, number and string values as objects, which changes them from primitive to reference type.

Consider the following:

Problems start to arise if you try to use a string object like a string primitive.  Keep in mind that a string object is still an object, and behaves like one.

For instance:

Given that JavaScript will allow you to use the methods associated with an object prototype on the primitive values (example below), it is best to use primitive types whenever possible.

FYI, Effective JavaScript by David Herman has a good section on why primitive types should be preferred to using object wrappers and I derived some of my examples above from those found in his book.

Ok, enough about Primitive Types, hopefully this has been more than enough info to solve ‘Boo who’.

End of Tangent

Check out the Mozilla link above for further explanation of primitive values (or this one for info on the Boolean object), but for our purposes in solving ‘Boo who’ we now know that the only boolean primitives are true and false.

So, now we have to fill in the booWho function above, checking if bool equals true or false.  In doing so, we’ll use the strict equality operator (===) vs. the loose equality operator (==), as the latter will convert the two values to a common type before checking equality, which is not what we want.

At this point, we’ve gone over all the tools you’ll need to write the booWho function, so if you’re working through the freeCodeCamp curriculum you may want to go try to solve it on your own, otherwise, read on…


Here’s the solution to ‘Boo who’ that I came up with:

As you probably know, || is the logical operator ‘or’ in JavaScript, so this function will return true if bool equals true or false.

The freeCodeCamp wiki provides an even shorter solution using the ‘typeof’ operator, written by Rafase282.

Both solutions solve the ‘Boo who’ algorithm challenge and pass all the test cases at freeCodeCamp.

That’s it for now, hopefully this helped straighten out the code…


My Learn-to-Code Journey: 3 months in…

I started my learn to code journey in late February, 2016,  when I signed up for freeCodeCamp.  So it’s been about 3 1/2 months and I figured this was as good a time as any to share my thoughts, progress, tools that I’ve found valuable, etc, etc.

First, a little background on what led me to this journey…

I’ve been doing drug discovery/pharmaceutical research for nearly 20 years.  Last year, I found out that my job was moving to a different part of the country and for many reasons, not the least of which was apprehension over relocating my two sons, we decided not to make the move.

After a lot of soul searching (and driving my wife crazy), I decided to shift my career and learn to code.  In fact, the real light bulb moment was reading Derek Sivers post ‘Should you learn programming? Yes.’  It’s where I discovered freeCodeCamp and everything seemed to snowball from there…

Stumbling on that Sivers post was just what I needed and I’ve since become a huge fan of his.  If you haven’t read or watched anything by Derek, do yourself a favor and check out his site. He has a unique perspective on life that’s completely refreshing when compared to most of what you encounter in the world today.

Ok, enough background…

Looking back at the last 3 months, it’s hard to put into words everything I’ve gone through in learning to code.  So far the journey has been fun, frustrating, time consuming, enlightening, tiring, exciting, stressful, rewarding, humbling…  Basically, it’s been a roller coaster ride.

It seems like one minute I feel like I’m on top of the world and that I can do and create anything.  Then the next minute I feel like a total idiot and don’t know anything.  Then genius again, then an even bigger idiot than before…  You get the picture.

Free Code Camp Update

For the time being, I still have a full time job (and coach my son’s baseball team) so I don’t have as much time as I’d like to devote to coding.  I’ve put in a lot of late nights and weekends and, despite the two huge time commitments mentioned previously, feel like I’ve been able to make a ton of progress.

I’m nearing completion on the Front End Development Certificate at freeCodeCamp.  I’ve completed all of the Algorithm Scripting Challenges and most of the Development Projects.  The only projects that I have left are to finish my Tic Tac Toe game, then build a Pomodoro Clock and a Simon game.  These, obviously, will take some time, but I should get them done within the next month or so.  You can check out my projects on my Codepen page.

I’ve also started on the Back End Development Certificate.  The first lesson was on Git, then you get right in to Node.js and Express.js.  The biggest thing I’ve learned so far on the back end (in addition to reinforcing once again that I’m an idiot) is that I need to become comfortable in the command line.

Thoughts on Free Code Camp thus far..

I feel that freeCodeCamp, in providing projects with increasing levels of difficulty, provides a great path to follow in learning to code. On the other hand, I feel some of the learning modules are lacking.  For instance, having gone through the git and npm learning modules at the beginning of the back end certificate, I don’t feel like I learned much and have little more understanding of how to use them now than when I started.

I don’t think, however, this is a negative for freeCodeCamp (and not sure if it was purposefully constructed this way or not).  After all, what is the purpose of freeCodeCamp??  To prepare you to become an employable full stack web developer?

Part of this whole process is learning to become resourceful and knowing where and how to look for answers when you run into problems. Something at which I’ve become increasingly skilled.  Is it frustrating to pause in the middle of a project to go look up how to code something? Absolutely.  Over and over again?  Yup…  But in the end, I’ll be a much stronger programmer for it.

In addition to Free Code Camp

In a previous post, I mentioned some of the resources I’ve been using as I make my way through the front end cert at FCC.  In addition to these, I’ve been broadening my knowledge of software development in general through the use of other sites, listening to podcasts and constantly searching for ways to learn (including starting a blog!!).

One of the other sites I discovered is Codewars.

Having finished all 3  algorithm sections at FCC, I was looking for a way to further my algorithm solving skills.  I haven’t used the site extensively yet, in fact, I’ve only worked through about 4 or 5 ‘kata’ (which is what Codewars calls their algorithm problems), but I plan on going back on a fairly regular basis to stay fresh with Javascript as I work through the backend at FCC.

After you solve a given ‘kata’ , you can see the solutions of previous users. In fact, I’m planning on writing one of my next posts on an embarrassingly long solution I wrote that others had solved in one line. Ha!  Live and learn I guess…

Like I said before, I’ve also gotten in the habit of listening to podcasts on my commute to and from work.

Some of my favorites, in no particular order are: JavaScript Jabber, CodeNewbie, Coder Radio, Eat Sleep Code, Becoming a Data Scientist, CodePen Radio, Simple Programmer, Software Engineering Daily and anyone that interviews either Seth Godin or Derek Sivers (The Tim Ferriss Show has great interviews of them both).

More Learning Tools

As I said before, I’ve used a lot of the tech websites (Mozilla, Stack Overflow, W3schools, etc) to learn as I went through the Front End course. I’ve decided to try something different on the back end.  I signed up to audit a Node.js course on coursera.  I can’t give any feedback on how useful the course is or whether it will help with the back end cert at FCC, as I just signed up for it yesterday, but I’ll be sure to write a future post about it.

While listening to the tech podcasts mentioned above, I kept hearing, over and over, the advice to create a blog.  So I did…  And I’m happy I did.

I have no idea how many people this blog will reach in the end, but one of the great benefits of it has been in reinforcing everything I’ve been learning.  I know one of the best ways to really learn something is to try to teach it to others, but I didn’t realize just how true this was.

Even in writing a post about the solution to a Basic Algorithm Scripting challenge, I’ve really had to solidify my knowledge of the JavaScript language and understand the methods and data types that I’ve used.  So, if you want to check if you really understand something, try explaining it to someone else…

As for advice on creating the blog itself, Simple Programmer (John Sonmez) created a great, and free, blogging course that walks you through the steps of creating a blog.  If you’re considering creating your own blog, I recommend signing up for his course, it’s what I used to get started.

Damn this is post is long…

On the remote chance that anyone has stumbled on this blog, and on the even more remote chance that you’re still reading, let me leave you with this..

If you’re on your own journey in learning to code, you are going to go through some rough patches and getting though those times are going to be extremely difficult.  My advice is this…  Don’t take the easy way out, it’ll just lead to more rough patches.

What I mean by that is if you are looking to solve problem x, or write some code to accomplish y, the easy way would be to google it, find some code that someone else has written, copy and paste it into your project and move on…  This may solve your short-term problem, but will get you nowhere in the end.

I’m not saying don’t use google to solve problems or look up ways to write something in whatever language you are using at the time.  I do that all the time.  What I am saying is take the time to learn how the code is written, or the logic behind the algorithm and go write your own solution…

That’s all for now, I’m off to continue my journey…









FCC ‘Seek and Destroy’

This post will work through ‘Seek and Destroy’, one of freeCodeCamp‘s basic algorithm scripting challenges.  This challenge will help us introduce the Arguments object and callback functions.

The Seek and Destroy challenge states that ‘provided with an initial array (the first argument in the destroyer function), followed by one or more arguments’ we need to write a function that will remove the values of all the arguments given after the initial array, from the initial array.

Sounds confusing, I know…  Let’s visualize it to clear things up.


One of the major problems we have lies with ‘followed by one or more arguments’…  If we don’t know how many arguments there will be, how do we write a function to handle them?

The answer to that comes in the form of the Arguments object.  JavaScript provides a local variable ‘arguments’ that acts like an array with an element for each argument passed to the function.  You access the arguments just like you would an element of any other array like so:

The arguments object makes solving this algorithm challenge much easier.  Looking forward at ways to solve this problem, we need to a) access the arguments we need to delete from the initial array and b) somehow remove those arguments from the array.

Storing arguments for easy access

The first thing we’ll do is create a new array and store all the arguments (other than the initial array) in it.

As you can see, arguments can be treated like any other array…  Here, we used the length property and looped through arguments (starting at element 1 so as to avoid the initial array, which will be passed in as the first argument, or arguments[0]).  Setting it up this way will handle any number of arguments passed to the destroyer function.

The push() method in JavaScript is used to ‘push’ new elements on to the end of the calling array.

Tools we’ll need to remove arguments from the array

In order to remove the arguments from the initial array, we will use the filter() method.  The filter method introduces a concept we haven’t talked about here at CrookedCode: the callback function.

A Quick note on functions in JavaScript

The power of functions, and what they allow you to do, are core to successful JavaScript programming.  Functions can be passed to and returned from other functions.  They can also be stored as variables.

A function that accepts another function as an argument is called a higher-order function, whereas, the function that is passed as the argument is called a callback function.  The Eloquent JavaScript chapter on functions is a great tutorial on the use of functions in JavaScript.

Eloquent JavaScript also devotes an entire chapter to higher-order functions.  I highly recommend reading and working through the examples in both of these chapters.  If you’re looking for something a little quicker, JavaScriptissexy.com has an excellent article on understanding callbacks and higher order functions.

The filter() method accepts a callback function and performs that callback on every element in the calling array.  If the callback returns true for a given element, that element is kept.  If the callback returns false, obviously, that element is not kept.

As stated above, filter() calls the callback function on every element of the calling array, but it does not mutate that array.  Therefore, you must save the kept elements as a new array.

The one final tool we’ll need before we try to solve this algorithm is the array.indexOf() method.  indexOf() will return the first index at which a given value is found.  If the given value is not found, indexOf() returns -1.

Putting it all together…. (finally)

We’ve already created argArr[] to hold the values that we need to delete from the initial array.  We now need to write a callback function for filter() that will return true if we want the element to stay and false if we want to delete that element.

We know that indexOf() will return -1 if a given value is not found.  We also know that we want to keep elements of the initial array that aren’t found in argArr[].  So, for each element of the initial array, lets call argArr.indexOf(), and return true for values of -1.  Therefore, we’ll be returning true for (and keeping) values that aren’t in argArr[].

Like so:

Wow…  This post covered a bunch of stuff we haven’t previously gone over here at CrookedCode, hopefully this helped straighten out the code for you.  See you next time…


FCC ‘Slasher Flick’

Today we’re going to work on freeCodeCamp‘s Basic Algorithm Scripting challenge – ‘Slasher Flick’.  This will be a pretty short post as we’re really not going to cover anything new,  I just wanted to go through this algorithm to show how short and concise a solution to a problem can be.

The ‘Slasher Flick’ challenge is to ‘return the remaining elements of an array after chopping off n elements from the head’.

This is the starting point FCC gives us:

As you can see, we need to write the body of the function ‘slasher’, which accepts an array ‘arr’ and a number ‘howMany’.  slasher should then return ‘arr’ with ‘howMany’ elements removed from the head, or beginning, of the array.

We can actually accomplish this challenge with just 1 line of code.  To do so, we’ll use the array.slice() method, which we first used in the ‘Title Case a Sentence’.

If you remember from that previous post, array.slice() is a method that returns a new array (which is what we need this function to return).  It takes 2 arguments.  The 1st argument is the index of where the returned array will begin, which is conveniently given to us as the argument ‘howMany’.  The 2nd argument for array.slice() is the index of the final element to include in the returned array, and is optional.  Since we’ll be returning the entire array after cutting off ‘howMany’ elements, we’ll only use 1 argument.  If you wanted to include ‘arr.length’ as the second argument to make it more clear, that would work too.

Now, given we have everything we need to return the wanted array, here’s the solution:

This function now returns the given array with ‘howMany’ elements removed from the head.

Obviously this algorithm is pretty simple, it’s part of the ‘Basic Algorithm Scripting’ after all, but I wanted to cover it to show that sometimes the solution can be pretty straightforward and simple…  No need to overthink it.

Hopefully this helped straighten out the code for you.


FCC ‘Title Case A Sentence’

Today we’re going to work through another of freeCodeCamp’s Basic Algorithm Scripting challenges – ‘Title Case A Sentence’.  To solve this challenge we must write a function that accepts a string, then returns that string with the first letter of each word capitalized and the remainder of each word in lowercase.

We’ll be using concepts that we’ve covered in previous posts (i.e. for loops, string methods, and arrays) and adding a couple new things like method chaining and some new string methods.

As I’ve mentioned previously, in JavaScript, the data type String has many methods associated with it.  W3schools has a good intro to these methods, MDN covers the string object on a deeper level.

Here’s the starting point given to us by freeCodeCamp.

We’ll start solving this challenge by breaking the given string (str) into an array of words, the same way we started ‘Find the Longest Word in a String’.

Quick tangent – we haven’t discussed yet that JavaScript can be tested right in your browser.  If you have a question about what a snippet of code does, just plug it into the console of your browser and test it.   To make sure the str.split() method that I just created is doing what I want, I plugged it into the console in Chrome and tested it.

As you can see, str.split(‘ ‘) is splitting the given string into an array, with each word as an individual element.  Now I’m confident in continuing with the solution to the algorithm challenge…

Next, we need to iterate through the array and capitalize the first letter of each word and lowercase the rest of the word.  To iterate through the array, we’ll use a for loop.  To handle the upper/lowercasing of the words, we’ll need a couple new string methods.

For a deeper understanding, and a list of many more string methods, check out the MDN page on strings.  Today, we’ll be using charAt(), toUpperCase(), toLowerCase() and slice().

toUpperCase() and toLowerCase() do exactly what they sound like, they return the calling string value in upper and lower cases respectively.  charAt() allows you to access a specific character of a string and takes one parameter – the index of the character you want to access (indexes of strings start at 0, just like arrays).

slice() allows you to access a specific segment of a string.  It takes two arguments, the indexes of where you want the segment to start and finish.  The second parameter is optional, if you leave it out, the slice will start at the first index you provide and continue to the end of the string.  Very important – all four methods mentioned above return the value of a string, they do not change the calling string.  See code below for demonstration…

Now, as mentioned above, we can chain these methods together using dot notation, so that after one method is called, a second method is called on the returned string of the first.

Check out this post on method chaining for a better understanding.

We can use everything we’ve talked about to get the first character of a string and capitalize it, then get the rest of the string (starting at index 1) and lowercase it.  Putting it all together looks like this:

Several notes on the above solution…

We used the join() method, which does the exact opposite of split.  join() is an array method that returns a string delimited by what you pass as an argument.  In this case, we gave an empty space (” “) as the argument, so each element of the array was joined together separated by a space.

Also, you will note that we used a ‘+’ to connect 2 parts of a string together.  This is an easy way in JavaScript to build a string.  It can be used with both literal strings and variables as such:

Hopefully this straightened out the code for you…  See you next time.