Confirm the Ending -- Yet another approach

Of course, when I saw the solutions to this challenge I felt like ‘Duh!’ Yet, I want to share this for the sake of getting feedback (positive or negative will be appreciated):

function confirmEnding(str, target) {
  let ending = [];
  for (let i = 0; i < [...target].length; i++) {
    ending.unshift([...str][[...str].length - 1 - i])
    }
    let endingStr = ending.join("")
    return (endingStr === target);
    }

confirmEnding("Bastian", "n");
console.log(confirmEnding("Bastian", "n"));

Why didn’t I think about the slice()? I don’t know! I must rework these methods…

Can you share the problem description or the link to the problem description?

Sure:

It seems the question wanted you to use something like substr function?

So maybe you can look at that as well.

1 Like

I am most thankful with your feedback and will solve the problem again in a much more straightforward way and of course I will study strings more seriously.

Thanks a lot! You rock!

1 Like

OK, I did the job that you recommended and came up with this (hope you like it):

function confirmEnding(str, target) {
let ending = "";
 for (let i = 0; i < target.length; i++) {
    ending = ending + str[(str.length - target.length) + i]
    }

return (ending == target);
    }

confirmEnding("Bastian", "n");

console.log(confirmEnding("Bastian", "n"));
console.log(confirmEnding("Congratulation", "on"));
console.log(confirmEnding("Connor", "n"));
console.log(confirmEnding("Walking on water and developing software from a specification are easy if both are frozen", "specification"));
console.log(confirmEnding("He has to give me a new name", "name"));
console.log(confirmEnding("Open sesame", "same"));
console.log(confirmEnding("Open sesame", "sage"));
console.log(confirmEnding("Open sesame", "game"));
console.log(confirmEnding("If you want to save our world, you must hurry. We dont know how much longer we can withstand the nothing", "mountain"));
console.log(confirmEnding("Abstraction", "action"));

Thanks a lot for your honest feedback.

1 Like

Indeed, this is cuter:

function confirmEnding(str, target) {
  let ending = str.substring(str.length-target.length, str.length);
  return (ending == target);
}

confirmEnding("Bastian", "n");

console.log(confirmEnding("Bastian", "n"));
console.log(confirmEnding("Congratulation", "on"));
console.log(confirmEnding("Connor", "n"));
console.log(confirmEnding("Walking on water and developing software from a specification are easy if both are frozen", "specification"));
console.log(confirmEnding("He has to give me a new name", "name"));
console.log(confirmEnding("Open sesame", "same"));
console.log(confirmEnding("Open sesame", "sage"));
console.log(confirmEnding("Open sesame", "game"));
console.log(confirmEnding("If you want to save our world, you must hurry. We dont know how much longer we can withstand the nothing", "mountain"));
console.log(confirmEnding("Abstraction", "action"));
1 Like

Thanks Leader @camperextraordinaire

function confirmEnding(str, target) {
	let ending = "";
	for (let i = 0; i < target.length; i++) {
		ending += str[str.length - target.length + i]
		}
	return ending == target;
}

Thanks a lot @camperextraordinaire . I do need to learn how to do that as it is more efficient. I will munch on that. You’ve taught me a lot.

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