// My solution - too much thinking leads to complexity
function confirmEnding(str, target) {
str = str.split(' ').reduce((acc,val)=>val).substr(-target.length);
return str===target;
}
var res = confirmEnding("Bastian is cool", "ol");
console.log(res);
// a simpler and obvious solution -
function confirmEnding(str, target) {
return str.substr(-target.length)===target;
}
// a more direct answer =
function confirmEnding(string, target) {
return string.endsWith(target); // added in ECMAScript 6
}
confirmEnding('Bastian', 'n');