Basic Algorithm Bonfires(BAB)[SPOILER]: verbose or one-liners?

I’m in the BAB section right now, I’m finding that I prefer to break down my solutions into one action per line as opposed to some clever one-liner. I do this because it helps me see visually what is happening.

Is this a bad approach? How would this look in a tech interview?

Example function:

 function titleCase(str) {
    var lowCase = str.toLowerCase();
    var lowSplit = lowCase.split(" ");
    var emptyArr = [];

    for (var i in lowSplit) {
      var temp = lowSplit[i];
      temp = temp.charAt(0).toUpperCase() + temp.substring(1);
      emptyArr.push(temp);
    }
    return emptyArr.join(" ");
}

AS someone who really struggles at first with functional programming methods such as map, filter and reduce, I now actively search for all opportunities to use them in my code.

Using map or reduce is not the same thing as using a one liner or creating a clever solution.

Map, filter and reduce are ways of encapsulating an operation on an array so that it performs as expected every time. Using for in looops or for loops to look through and act on elements of an array is more likely to introduce e typing errors.

In the end it’s a matter of readability and maintainability. In a month from now you may look at the same code and think differently.

Also I decided to give your original example a go: (Fig is my dog’s name). How much more challenging is this to read?

 function titleCase(str) {

  var arr = str.toLowerCase().split(" ")
  
  return arr.reduce(function (a,b) {
      return (a + b.charAt(0).toUpperCase() + b.substring(1) + " ")
    }, "").trim()

}

titleCase("fig is a rock star")

If I were hiring, i would want my programmers to feel comfortable with these operations. I think once you know & understand them through use, they read just as well and show more versatility as a programmer…pattern recognition of a problem as a reduce or filter function for example, means my hiree can solve problems faster

2 Likes

that’s a beautiful piece of code you got there!! how long you been coding? im still new, i wish i knew more for how long i’ve been going at it but when i went through the basic algorithm section i got help on just about every answer. read enough to know what was going on but didnt even give it an honest try to absorb it. needless to say i finished that section feeling lost and about gave up; really tried to blame it on FCC. After about a week (or two-three) i got back at it and google’d everything. I would go into the chrome console and create and manipulate variables until i got it. Huge leap in skills for me. So personally, i wrote long blocks of code, sometimes beating around the bush but it was always mine. I was so proud to get it to work that it really changed my motivation. it looked awful but it worked and i knew what each piece meant which meant alot.

2 Likes

You’re still getting a grasp on making algorithms so you’re not going to be perfect at it, but the code you wrote looks pretty novice. Imagine someone was learning to write for the first time while they were writing a book. There would be a lot of needless sentences that kind of get nowhere. ‘This is my story. It is about 3 people that are also friends with eachother.’ That’s what your code is like.

There’s no need to define two variables that each do something as self explanatory as toLowerCase and split. And there’s really no point in moving the items in the array to a new array just to capitalize them.

function titleCase(str) {
     str = str.toLowerCase().split(" ");

   for (var word in str){
       str[word] = str[word].charAt(0).toUpperCase() + str[word].slice(1);
   }
   return str.join(" ");
}

Though of course the cool guy way would be

function titleCase(str){
    return str.toLowerCase().split(" ").map(word => word[0].toUpperCase() + word.slice(1)).join(" ");;
}

I assume your are asking the original poster how long he has been coding?

But I just watned to say that your message is inspiring, and really important. That struggle is where the learnign takes place. It’s OK if it takes time to learn, because if you keep at it, you’ll get it !

I’ve been doing javascript for 2 years and still learning

1 Like

i like that, i hope i keep that in mind.

It’s not that much more challenging to read. I’m just gauging opinions.

I’ve been coding for 1.5 years.

I guess my main thing is that I like explicitness over implicitness.

I understand your points and how you made the function more succinct.

Without prior experience, your solution may not be apparent unless someone is willing to understand what is happening at each method chain.

The cool guy solutions annoy me sometimes only because I’ve been in tech interviews where those were the preferred solutions and if you didn’t get them, you were passed over.

you really shouldn’t be using for… in with arrays.

the indices are strings and there is not guarantee that they will iterate in order

I don’t expect that it would reflect well in an interview.

can read more here…

standard for loops can be also error prone, I think that the functional equivalents (forEach map, …) are safer, more expressive and should be preferred. They can also be more expressive when combined with other functions.

    str.toLowerCase()
        .split(' ')
        .map(titleCaseWord)
        .join(' ');

I guess my main thing is that I like explicitness over implicitness.

The thing is that .map, .filter, .reduce will become more explicit if you know how they are used. I definitely prefer the one-liners. It often takes a little longer to understand the single line, but with properly naming your functions and variables that is also minimized.

It is perfectly fine to have a more verbose answer the first time round, though. But later a shorter solution will become more attractive in my opinion.