Classy Coding

My Journey to Code

Javascript Under Pressure

Leave a comment

Yesterday, I completed the javascript under pressure game online. The game gives you 5 challenges with a series of tests that should pass if completed correctly all while there is a timer ticking away. This at first sounds intimidating however the timer doesn’t really mean anything. You can take as long as want so for me I didn’t even really look at the clock. What is helpful and worth your time is the fact that there are a series of tests that check your code. So your code may work for these simple cases however, there are more complex cases that your code will break on. This is fantastic because you it forces you to think more about how to make your code more generic and efficient. More in terms of general cases instead of specific set of cases that you are familiar. These helped me find methods in javascript that I had not seen before and how to structure my loops to make efficient use of my conditions. 

One example of this was challenge 3 where you were given a string that may or may not have a file extension. You needed to write a condition to output the extension if there was one or false if there was no file extension. This at first seemed straight forward however, it can quickly become tricky.  At first, I simply used the splice method by “.” and then since that puts it into an array you can quickly return the file extension through the index notation. However, you could also check using the indexOf to check the array for the presence of “.”

The last challenge was the most challenging. This challenge I learned a lot since javascript does not have classes. For example the challenge gave you an array and your code should return the sum of the array. However, this is no simple task since you could be given an array of arrays with strings or numbers so you need to find a to check for these conditions. My first attempt had nested for loops (quite horrendous to look at really) but it broke on the last test because it was an array like this: [ [[[[[[[[1]]]]]]], 4,5] therefore, my nested loop would break and the way I had structured my code I would need to know exactly how many times to loop through however, that didn’t seem to be the most efficient way to go about doing it. I realized I needed to have a recursive call for this iterative function that way it didn’t matter what I got so an array like this: [ [[[[[[[[[[2,3,]]]]]]]]]],4,5] would not break. Therefore, it actually ended up making my code shorter by doing this and I was able to take out the ugly nested loops. This helped me get more comfortable with different methods in javascript that I didn’t know about beforehand. That way when I have a more complicated problem I already have more tools in my toolbox now. Unfortunately, this website does not allow you to go back to the code you submitted otherwise I would have copied and pasted it in here to better explain. 

I’m going to post some more examples on my refactoring as I learn more in javascript. Stay tuned. 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s