Completing Algorithms

Tiffany White,

So, the last time I wrote an article here, I was struggling with the Truncate String algorithm.

I took to the forums again and a guy named Chuck told me to delete all the pseudocode. He talked to me about keywords and how to think about what was being asked of me. He broke it down like this:

Oh wait! Three? '...' is three dots! so we can change "the length of '...' in our plan into three!:

If the length of str is longer than the value of num then shorten str to be as long as the value of num minus three. Unless the value of num is less than or equal to three. In that case, just shorten str to be as long as the value of num. Either way, then add '...' to the end of str and give it back to us. Otherwise, just give us back str.

A ha! That made sense.

My code got a little better, but it wasn’t quite right. I went to the Gitter chat. I was struggling with getting the right truncated string. All but two tests were passing.

I got guided in the Gitter chat, but didn’t get the answer given to me, which I really loved. [Tweet " Completing algorithms without being given the answer is extremely rewarding and satisfying."] My solution:

I was reading on one of the Free Code Camp groups about Andrew Charlebois, a guy who took his Free Code Camp skills and in 5 months got a job as a Front-end dev.

I was curious about him— I loved his portfolio and so I hooked up with him on LinkedIn and checked out his Free Code Camp profile.

I looked at his solution for Truncate String and his was elegant. It looked like this:

Wow.

One of the things I got advised of in our Gitter chat was that you need to write the stricter conditional first, in this case if (num <= 3) {} instead of if (str.length < 3). But if you look at Andrew’s solution, it isn’t true. Maybe that is because he used <= in the conditional, but it shouldn’t matter.

I knew, also, that I didn’t need to actually use slice() but I didn’t know which of the stringmethods I could use. Andrew used substr()which is probably why it looks so clean.

So, the last time I wrote an article here, I was struggling with the Truncate String algorithm.

I took to the forums again and a guy named Chuck told me to delete all the pseudocode. He talked to me about keywords and how to think about what was being asked of me. He broke it down like this:

Oh wait! Three? '...' is three dots! so we can change "the length of '...' in our plan into three!:

If the length of str is longer than the value of num then shorten str to be as long as the value of num minus three. Unless the value of num is less than or equal to three. In that case, just shorten str to be as long as the value of num. Either way, then add '...' to the end of str and give it back to us. Otherwise, just give us back str.

A ha! That made sense.

My code got a little better, but it wasn’t quite right. I went to the Gitter chat. I was struggling with getting the right truncated string. All but two tests were passing.

I got guided in the Gitter chat, but didn’t get the answer given to me, which I really loved. [Tweet " Completing algorithms without being given the answer is extremely rewarding and satisfying."] My solution:

I was reading on one of the Free Code Camp groups about Andrew Charlebois, a guy who took his Free Code Camp skills and in 5 months got a job as a Front-end dev.

I was curious about him— I loved his portfolio and so I hooked up with him on LinkedIn and checked out his Free Code Camp profile.

I looked at his solution for Truncate String and his was elegant. It looked like this:

Wow.

One of the things I got advised of in our Gitter chat was that you need to write the stricter conditional first, in this case if (num <= 3) {} instead of if (str.length < 3). But if you look at Andrew’s solution, it isn’t true. Maybe that is because he used <= in the conditional, but it shouldn’t matter.

I knew, also, that I didn’t need to actually use slice() but I didn’t know which of the stringmethods I could use. Andrew used substr()which is probably why it looks so clean.

I have started on PluralSight’s JavaScript Track. I took their test to see how much JavaScript I know and I tested at the low-end of proficient, which makes me very happy. I want to get this down— Prototypal Inheritance and Objects before I start into ES6. Lots of syntactic sugar in there. I like it.

© tiff.RSS