Skip to content

Instantly share code, notes, and snippets.

@JonathanZWhite
Created May 19, 2018 06:59
Show Gist options
  • Save JonathanZWhite/bec91c5f91fa7a82e257bf127c2bb427 to your computer and use it in GitHub Desktop.
Save JonathanZWhite/bec91c5f91fa7a82e257bf127c2bb427 to your computer and use it in GitHub Desktop.
/**
* Given the offset and length of characters to replace, as well
* as the words to replace them with, write a function that produces
* the new sentance.
*/
const case1 = 'The adjective animal sat on the object';
const case1Entities = [
{ offset: 4, length: 9, value: 'fat' },
{ offset: 14, length: 6, value: 'cat' },
{ offset: 32, length: 6, value: 'mat' }
];
const case2 = 'ab bc ab abc';
const case2Entities = [
{ offset: 0, length: 2, value: 'All' },
{ offset: 6, length: 2, value: 'is' },
{ offset: 9, length: 3, value: 'well' },
]
const case3 = 'rvox ay rvox, 3qm, ay 3qme0hac';
const case3Entities = [
{ offset: 0, length: 7, value: 'All' },
{ offset: 8, length: 4, value: 'children' },
{ offset: 14, length: 3, value: 'except one' },
{ offset: 19, length: 2, value: 'grow' },
{ offset: 22, length: 8, value: 'grow' },
]
solution(case1, case1Entities); // The fat cat sat on the mat
solution(case2, case2Entities); // All is well
solution(case3, case3Entities); // All children, except one, grow up
@bamoha
Copy link

bamoha commented May 19, 2018

the function takes two arguments.. first is the string that is given.. second is the case entities object. then you split the object into an array.. attach a negative sign to the offset and use it to find the index of the value to be changed in the array. after which you replace it and return the joined string.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment