Check if a string ends with a given suffix, without using .endswith()
You could use substr and pass it a negative number. Doing so is the equivalent of str.substring(str.length - X). That means you just have to pass -target.length.



function confirmEnding(str, target) {
  return str.substr(-target.length) === target;
}

function test(str, target, expected) {
  let testName = `"${str}" ${expected ? 'ends' : 'does not end'} with "${target}"`;
  if (confirmEnding(str, target) === expected) {
    console.log(`PASS: ${testName}`);
  } else {
    console.log(`FAIL: ${testName}`);
  }
}

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