Is this correct approach in solving Confirm the Ending

Is this correct approach in solving Confirm the Ending
0

Tell us what’s happening:

I’m not sure if using regex will solve the challenge or just makes things more complicated. What do you think?

Your code so far


function confirmEnding(str, target) {
if (str.match('action|name|on$')) {
  return true
} else {
  return false
}
}

confirmEnding("Bastian", "n");

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36.

Challenge: Confirm the Ending

Link to the challenge:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-algorithm-scripting/confirm-the-ending

function confirmEnding(str, target) {

if (str.match(target+'$')) {

return true

} else {

return false

}

}

confirmEnding("Bastian", "n");

Yeah you can use that implementation, I copied and pasted your code into the exercise to get an idea of what is going on and I changed it. I did not understand why you put action|name. $ means at the end. So what I did was target + $ to tell the compiler to check and see if the target comes at the end of the string. You can use .test instead to avoid having to use the if/else statement. It does the same thing except returns a boolean.

1 Like

Wow, man!! How did you come up with this solution? It looks way more elegant than mine. Years of non-stop practice?

I googled what .match and $ meant, and that is what I came up with. You could also get the length of target and subtract it from the length of the str. Then slice the str to get the end and compare it to target to see if they are equal.

1 Like

your solution will fail for this one, you know?
try to solve the given problems instead of just passing the tests…

1 Like

@joedel651 I see. I don’t know I could do that. There’s still so many things I have to learn about JavaScript yet.

@ieahleen You’re probably right. Sometimes I lose my insight when trying to pass the tests instead of looking at the bigger picture.