Basic Algorithm Scripting - Confirm the Ending

Hi, calling the function with the requests give the same result of the requests, but the code doesn’t pass.

function confirmEnding(str, target) {
let a = target + "$";
let b = new RegExp (a);
console.log(b.test(str));
if(b.test(str)){
  return true;

} 

}


confirmEnding("Bastian", "n");



  **Your browser information:**

User Agent is: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:102.0) Gecko/20100101 Firefox/102.0

Challenge: Basic Algorithm Scripting - Confirm the Ending

Link to the challenge:

The function should return either true or false. I’m only seeing where it returns true.

I got confused because in the console it gives you false even without specifying the else statement. Thanks.

If you are referring to this line in your code:

console.log(b.test(str));

That will display either true or false because the method test returns either true or false. So knowing that, do you think you could shorten up this function? The test method is doing all the work for you. It returns exactly what you need your function to return. How can you use that to make your function cleaner? I’ll give you a hint, you only need one line of code after the two let statements.

I forget to erase the print statement, if you meant that.

function confirmEnding(str, target) {
let a = target + “$”;
let b = new RegExp (a);
if(b.test(str)){
return true;
} else{
return false;
}

}

confirmEnding(“Bastian”, “n”);
confirmEnding(“Connor”, “n”);

No quite what I meant. Your solution is perfectly fine, it works. But it has more lines of code than it really needs. As I mentioned in my last post, you can literally solve this with just one line of code (a return statement) after the two let statements. So total lines of code is 3. Remember, the test method is already returning exactly what you want your function to return. Let it do all the work for you.

Got it. Thank you!

function confirmEnding(str, target) { let a = target + "$"; let b = new RegExp (a); return b.test(str); }

confirmEnding(“Bastian”, “n”);
confirmEnding(“Connor”, “n”);

And then of course you could combine your two let statements into one :slight_smile:

function confirmEnding(str, target) {
let a = new RegExp (target + “$”);
return a.test(str);
}

confirmEnding(“Bastian”, “n”);
confirmEnding(“Connor”, “n”);

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