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?

There is nothing to be gained from making all of these [...target] and [...str] other than making your solution more complicated than necessary. Strings already have a length property and you can reference a string index the same as you can an array. You learned all of this back in the following challenges:

1 Like

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.

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

You do not need the parentheses around the str.length - target.length and you dod not need the parentheses around ending == target. Lastly, instead of ending = ending +, why not use the += operator here?

1 Like

Thanks Leader @RandellDawson

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

Good job! Now see if you can create a solution that returns false as soon as there is a divergence between the target and the string ending. In other words, don’t wait until you have iterated through target to determine if the ending is a match.

1 Like

Thanks a lot @RandellDawson . 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.

The solutions you created are fine. I was just pushing you a bit more.

1 Like