# Finding a Solution in An Abnormal Way, Good or Bad?

Hi all! My name is Carter Woetzel and I am your average fella’ workin’ my way through FCC. I am at the basic algorithm challenges.

I currently just finished #5 , Title Case a Sentence. I found a solution all on my own (which I am really proud of, its always an amazing feeling when you find a solution on your own!) but when I checked others answers, their answers did not look like mine. I am worried that if I am always finding abnormal ways to find a solution, I will miss out on the ‘correct’ way. I am afraid I will find solutions with gaps in my learning if that makes sense. Here was my solution to Title Case a Sentence.

``````function titleCase(str)
{
var hold;
var hold2;
var hold3;
var caps;
var finalString;
var _array=[];

_array = str.split(" ");

// 0=I'm 1=a 2=little 3=tea 4=pot
for (var i=0; i < _array.length; i++)
{
hold = _array[i]
hold3 = hold[0].toUpperCase();

console.log(hold3);
console.log(hold);

for (var j=0; j < hold.length; j++)
{
caps =(hold.slice(1 , hold.length)); //Slices after first index
caps = caps.toLowerCase();
}

hold2 = hold3 + caps;
console.log(hold2);
_array[i]=hold2;
}

//Now we Need to rejoin together all the words in the array with one space into 1 String
_array = _array.join(" ");

return _array;

}
``````

This code worked just fine, but when I examined other’s work, they had methods for solutions as well as recursion. Should I be worried? I am going to go back to project #1 and redo again with a method approach, I just am scared that I have gaps in my learning that will persist as long as I keep finding “shortcuts”! Thanks for reading, this was my first ever forum post on FCC so hopefullly I didn’t mess it up!

I don’t really like the idea of abnormality in code but I’ll give it a shot.

If abnormal code results more efficient and performant code, which will enhance the quality of your application, then abnormal code is good. In most other case, however, abnormal code is bad, all they do is confuse other programmers. (But this requires vague definition of what abnormal is and you can only get this via experience and observation.) In the end, you should be aware of any abnormality and decide whether it is absolutely necessary.

Your code is only abnormal by the appearance and it can be fixed easily with better understanding of the programming concept, language, and idioms. However, in terms of approach to the problem, your code is not abnormal. For example, try isolating your algorithm from your code–doing this often tells whether your algorithm is non-intuitive, complicated mess. (Although, sometimes making a mess is the only recourse)

Apparently, your method can be summarized to this:

1. Extract words from string.
2. For each word, make first character to upper case and rest to lower case.
3. Join transformed words back to sentence.

It doesn’t sound abnormal in any way. (it’s direct translation of what title case is). Now, check this code,

``````function titleCase(str) {
var words = str.split(' ');
for (var i=0; i < words.length; ++i) {
var word = words[i];
words[i] = word[0].toUpperCase() + word.slice(1).toLowerCase();
}
return words.join(' ');
}
``````

It is not the most elegant code, but it uses the same method that you’ve used.
Is it correct? it will give you the desired answer, so it’s correct within the domain of the problem.
Is it more concise and readable? This can vary on people; some will expand it, shorten it, or might even use utterly different tools. So, you will have to draw line somewhere.
In my view, it’s better than yours because I haven’t use ambiguous variables such as hold1,2,3, whose meaning is not immediately clear and I have combined upper-casing and small-casing in one loop to express them in a single concept.
The code isn’t too far from what you’ve done, all I’ve done is to remove few unnecessary variables and combine some operations.

Honestly, I wouldn’t call your code abnormal; it is just not idiomatic and unedited, which is expected. Since you are willing to upgrade your code, it will get better as you learn more. The real problem is habit of writing code in extremely complicated manner but you don’t really seem to have much of that issue. So, just keep learning.

3 Likes

Thank you so much for the response! Really appreciate it. I will power through, its good to know I am on the right track. Idiomatic and unedited, that really hits it on the mark.