Edabit

Given a letter, created a function which returns the nearest vowel to the letter. If two vowels are equidistant to the given letter, return the earlier vowel.

Examples

nearestVowel("b") ➞ "a"  nearestVowel("s") ➞ "u"  nearestVowel("c") ➞ "a"  nearestVowel("i") ➞ "i"

Notes

  • All letters will be given in lowercase.
  • There will be no alphabet wrapping involved, meaning the closest vowel to "z" should return "u", not "a".

Solution:

 1  2  3  4  5  6  7  8  9 10 11 12 13
function nearestVowel(s) {   let obj = { a: 99, e: 99, i: 99, o: 99, u: 99 };   let min = 99;   let sol = "";   for (let char of [..."aeiou"]) {     let diff = Math.abs(char.charCodeAt(0) - s.charCodeAt(0));     if (diff < min) {       min = diff;       sol = char;     }   }   return sol; } 

This free site is ad-supported. Learn more