code.ashish.me

Atom feed

Recently added: 02 Count Of Subset Sum, 416 Partition Equal Subset Sum, 01 Subset Sum, 518 Coin Change 2, 983 Minimum Cost For Tickets

034 Confirm Ending

/**
 * 
 * Ashish Patel
 * e: ashishsushilPatel@gmail.com
 * w: https://ashish.me
 *
 */

/* 
Check if a string (first argument, str) ends with the given target string (second argument, target).

This challenge can be solved with the .endsWith() method, which was introduced in ES2015. But for the purpose of this challenge, we would like you to use one of the JavaScript substring methods instead.

Remember to use Read-Search-Ask if you get stuck. Write your own code.

Example

confirmEnding("Abstraction", "action") returns true;
confirmEnding("Open sesame", "pen") returns false;
*/

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

test('confirm Ending', () => {
  expect(confirmEnding('Abstraction', 'action')).toEqual(true)
  expect(confirmEnding('Open sesame', 'pen')).toEqual(false)
});

Created 2019-12-11T02:35:21+05:18 · Edit