Episode 13 - The Sharpening

Episode 13 – The Sharpening

In this episode, we dive a into some code katas and walk through a coding exercise. We’ll talk about sites we like to challenge ourselves with to keep our skills sharp.

What is a code kata? It’s a term for an exercise coined by Dave Thomas (not the Wendy’s guy) in the Pragmatic Programmer. The name kata is a Japanese word referring to the practice of martial arts. Code katas are short coding challenges, typically with a handful of tests to pass, that are a great way to practice.

Challenge websites we’ve used:

Freecodecamp

Codewars

Exercism

Hackerrank

 

For more, look here: https://medium.freecodecamp.org/the-10-most-popular-coding-challenge-websites-of-2016-fb8a5672d22f

 

Now we’re going to walk through a couple exercises together!

Is n divisible by … – from CodeWars

CodeWars Exercise – Step 1:

Capture the first argument passed in using the argument object. Set it to a variable (n).

CodeWars Exercise - Step 1

CodeWars Exercise – Step 2:

In a for loop, iterate through the arguments object which will iterate through all of the parameters being passed in (we don’t know how many that will be, since it’s variable).

CodeWars Exercise - Step 2

 

CodeWars Exercise – Step 3:

Using an if statement and modulo (%), check to see if n is divisible by each argument being passed into the function. If the result is not 0, return false, which breaks the loop and returns false for the whole function.

CodeWars Exercise - Step 3

 

CodeWars Exercise – Step 4:

Return true outside of the for loop if n is divisible by all variables.

CodeWars Exercise - Step 4

 

Keep in mind that there are many different ways to solve this exercise, and I may go back and refactor my solution at some point to make it more concise and use ES6.

 

Find the Longest Word in a String – from freeCodeCamp

freeCodeCamp Exercise – Step 1: Because I want to compare every word, I know I’ll need to iterate through them. The .split() string method can divide a long string into an array of smaller strings. I’m using the space between each word as a delimiter to neatly divide them all up.

screenshot from freeCodeCamp step one

freeCodeCamp Exercise – Step 2: I created a placeholder for the longest word length, initialized to zero. Then I’m going to use a .forEach() loop to iterate through one array item at a time, which I’ll refer to as ‘word’ in each iteration.

screenshot from freeCodeCamp step one

freeCodeCamp Exercise – Step 3: Now I want to compare each word’s length to my longest value, and if it’s longer, make that the new longest value. So I’m evaluating a conditional statement, and taking a different action depending on the result. There are a couple ways to do this, an “if / else” statement or a ternary operator. I’m showing both here, but they are doing the exact same thing.

screenshot freeCodeCamp step three

freeCodeCamp Exercise – Step 4: The ternary operator is much more concise once you understand the ‘?’ and ‘:’, so we’ll go with that. I’ll return the value of ‘longest’ after my loop is complete, and the tests pass. Here is the completed function:

screenshot of freeCodeCamp step four

Thanks for listening. We’d love to hear about your solutions!

 

Resources:

Stack Overflow

Mozilla Developer Network

Posted by Junior Developer

Leave a Reply