Making the script for rhyming words but this logic giving the wrong answers. displayRhymingWords() this function giving me wrong results kindly help any one to improve the logic
<script>
function fetchRhyme() {
const wordInput = document.getElementById('wordInput').value;
const resultDiv = document.getElementById('result');
if (!wordInput) {
resultDiv.innerHTML = '<p>Please enter a word.</p>';
return;
}
const rhymingWords = getRhymingWords(wordInput);
displayRhymingWords(rhymingWords, resultDiv);
}
function getRhymingWords(targetWord) {
// You can replace this array with your own list of words
const wordsArray = ['example', 'apple', 'orange', 'sample', 'rhyme'];
// Calculate Double Metaphone for the target word
const targetMetaphone = doubleMetaphone(targetWord);
// Function to check if two Double Metaphone values match
const metaphonesMatch = (metaphone1, metaphone2) => metaphone1[0] === metaphone2[0] || metaphone1[1] === metaphone2[0];
// Find rhyming words
const rhymingWords = wordsArray.filter(word => {
const wordMetaphone = doubleMetaphone(word);
return metaphonesMatch(targetMetaphone, wordMetaphone);
});
return rhymingWords;
}
function displayRhymingWords(rhymingWords, resultDiv) {
if (rhymingWords.length === 0) {
resultDiv.innerHTML = '<p>No rhyming words found.</p>';
return;
}
const rhymingList = '<ul>' + rhymingWords.map(word => `<li>${word}</li>`).join('') + '</ul>';
resultDiv.innerHTML = `<p>Rhyming words:</p>${rhymingList}`;
}
</script>
Thanks in advance

