Javascript basic algorithmic scripting challenge

I just did the “Confirm ending” challenge using this code

function confirmEnding(str, target) {
  let strArr = str.split('');
  let targetArr = target.split('');
 let j = 0;
  for(let i = str.length - target.length ; i < str.length; i++){
  if(strArr[i] == targetArr[j]){
      j++;
    continue;
  }
  else{
    return false;
  }
  }
  return true;
}

confirmEnding("Bastian", "n");

Then I opened the hints page and saw that the whole thing was done with only a one line code - and although my code works I can’t help but feel like I am not supposed to do things like I just did, am I wrong?

1 Like

Yes, you are wrong. You may have your solutions different to other solutions, but that’s their solution. Be proud of your own solution.

Sometimes, people might not even be able to find a solution to a problem, but you did!

Be proud!

2 Likes

As growing developers, learning how to accomplish a task with the limited knowledge we have is a good idea as it helps cement that knowledge and improves problem solving skills through experience, given you have time to spare. After doing that though, learning the proper way to accomplish said task is also a good idea if you plan to write “proper code”, which everyone should strive for.

With that said, you’re on the right path. While your method was not the best way, it worked. Well done. The next step is to learn how that one liner works, not just to memorize it, but to understand it so you can produce code of a similar caliber in the future.

3 Likes

Getting the code working is always the first priority. From there, we’re always going back and improving things we’ve written in the past.

2 Likes

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.