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…
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.
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"));
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"));