I need to return the index of the odd word out in an array of words(strings).
For example...
['junper', 'jumper'] should return 0
['Jumper'] should return 0
['coast', 'coast', 'coal', 'coast'] should return 2
['sofa', 'sofa', 'sofa', 'soft'] should return 3
I already have a worked solution but I am aiming to make this cleaner.
I have been thinking about whats the best way to go about returning the index of the word which occurs only once...
This is my aforementioned working solution:
function findWrongWord(householditems) {
if (householditems.length < 3) return 0;
function findOccurencesLength (array, toEqual) {
return array.filter(x => x === toEqual).length
}
let copyOfInitialArray = [...householditems];
let [, mostOccuredItem = ''] = copyOfInitialArray.sort((a, b) =>
findOccurencesLength(copyOfInitialArray, a) -
findOccurencesLength(copyOfInitialArray, b));
return householditems.findIndex(x => x ===
mostOccuredItem.split('').reverse().join(''));
}
['coat', 'coast', 'coal', 'coast']return 2?length?