My solution is not even close to the given solutions

Tell us what’s happening:
So once again I found a completely different way of approaching this problem. I know there are always a ton of different ways of solving the same problem, and I usually have a similar approach, but not this time.

The solution converts the letter to the ASCII number. I wasn’t sure That never even crossed my mind.

Is my solution somehow worse than the given solution, if so would you mind explaining why?

My Code


function fearNotLetter(str) {

let testStr = 'abcdefghijklmnopqrstuvwxyz';

let fullRegex = new RegExp(str);
let testRegex = new RegExp(str[0]+"\\w\+"+str[str.length-1]);
console.log(testRegex);

if(fullRegex.test(testStr)){
console.log("undefined");
return undefined;
}

else{

let shouldBe = testStr.match(testRegex);
console.log(shouldBe);

let matchArr = shouldBe[0].split('');
let splitStr = str.split("");

let missing = matchArr.filter(letter => !splitStr.includes(letter)).join('');

console.log(missing);
return missing;

}



}

fearNotLetter("stvwx");

Challenge: Missing letters

Link to the challenge:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/missing-letters

It seems to work for me. The only thing is that you should be returning the primitive type undefined, not the string “undefined”.

Why is it not as good? It seems clunky and arcane. And it seems heavier - those are some pretty heavy operations going on for such a simple problem. Of the “given” solutions, I like the first one: simple, easy to read, easy to understand. And it’s probably the most efficient too. It could be tightened up a bit, and loose the comments, but it’s a good solution.

Given a choice between a “sexy” solution and a simple and easy to read solution, all things being equal, I’ll take the latter every time. In the real world, good coders write simple, easy to understand code. When I have to write something complicated and difficult to read, I know I’m doing something wrong.

4 Likes

Thanks for the reply! I’m pretty sure I returned the type undefined. I put the console.log() throughout my code to check my work as I am solving the problem.

So at this point in my learning (all I know of JS is from working through the JS algorithm and data structure cert) do you think it would be a good practice to keeping working through the algorithm problems as best I can and once I find a solution then go and see what the given hints and solutions are and try to solve it that way too?

I just check the solutions to see if what I came up with was similar or, like in this case, totally different.

I just want to get as much as I can out of FCC.

I’m pretty sure I returned the type undefined .

Sorry, I was reading too fast and saw the console.log.

do you think it would be a good practice to keeping working through the algorithm problems as best I can and once I find a solution then go and see what the given hints and solutions are and try to solve it that way too?

I think that’s a great approach. Just write code. Write bad code. The only way to learn to write good code is to write bad code. Just keep writing and getting better. Do your best, then see what others did.

1 Like