[Solved] Insert a string. Create new string which is the mirror reverse of the inserted one around its center


The Basic Idea

So with this solution, it’s quite simple, you have your dictionary, then you make a slight change to each object within the dictionary, you basically add some string to it so that if a word is 'card', it’ll swap the order of the letters in such a way that when you search for 'drac', card will appear to be a similar word.

A bit more detail

When you search for words similar to what you’ve got in your dictionary, to prevent outputting exactly what you’ve already searched, when constructing a new array, it’ll push null onto the array if index x is the same word as the word searched.

Then, once the array has been constructed, it filters out all values set to null.

// Simple string sort function. 
const sort = s => [...s].filter(x => x != null).sort().join('');


// Helper function which will state whether or not to push 
// a string onto the array or push null onto the array. 
const push = o => w => sort(o) === sort(w) && o != w ? o : null;


// A function that takes an array of words and a word
// via the use of currying, then magic happens! 
const anagrams = d => w => d.map(o => push(o)(w)).filter(s => s != null);
  
	

// The proof is in the pudding... 
const dictionary = ['stranger','ngerstra','torarot','rotator'];
const word = 'ngerstra';
const otherWord = 'rotator';
const results = anagrams(dictionary)(word);
const otherResults = anagrams(dictionary)(otherWord);
console.log(results);
console.log(otherResults);

3

solved Insert a string. Create new string which is the mirror reverse of the inserted one around its center