I have a habit of glancing over the provided solutions for each challenge after I first complete it myself. I have found this quite discouraging as the provided solutions are short, cleaner, and look so much more efficient than my approach.
I am working hard on scribbling down problems on a piece of paper, breaking the problem down into mini pieces, and solving them one at a time. This however makes it seem like I am writing unnecessary/repetitive code.
For instance, when solving the Confirm the Ending challenge in the Basic Algorithm Scripting chapter, I ended up writing 19 lines of code. (Code at the end)All the provided solutions are only 3-4 lines long.
My code worked just fine but I was overwhelmed by the fact that my solutions are 5-6 times longer than the ones provided in FreeCodeCamp. As a result, I had some random questions pop into my head and wanted to hear your opinions on them.
Is this something I should be worried about early on?
Should I be focusing on refactoring my solutions as a newbie?
Do companies care how long my programs are when it comes to hiring?
Does writing shorter code make programmers stand out to their employers?
My Code:
/*
--Algorithm--
1. reverse the target word and store it in an array
2. reverse the string and store it in an array
3. Loop through the target array
4. Inner loop through the string array
(Only need to loop through string Array as many times as the length of target array)
5. If the values on both arrays(at the same indices) don't match: return false & terminate
6. If not, return true
*/
function confirmEnding(str, target) {
let storeTarget = [];
let storeStr = [];
for(let i=target.length-1; i>=0; i--){
storeTarget.push(target[i])
}
for(let i=str.length-1; i>=0; i--){
storeStr.push(str[i]);
}
for(let i =0; i<storeTarget.length; i++){
for(let j=0; j<storeStr[i].length; j++){
if(storeTarget[i][j] !== storeStr[i][j]){
return false;
break;
}
}
}
return true;
}
console.log(confirmEnding("Bastian", "n"))