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?
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.