Checking for vowels in array of numbers using JavaScript


Problem

We are required to write a JavaScript function that takes in takes in an array of numbers. If in that array there exists any number which is the char code any vowel in ascii we should switch that number to the corresponding vowel and return the new array.

Example

Following is the code −

 Live Demo

const arr = [5, 23, 67, 101, 56, 111];
const changeVowel = (arr = []) => {
   for (let i=0, l=arr.length; i<l; ++i){
      let char = String.fromCharCode(arr[i])
      if ('aeiou'.indexOf(char) !== -1){
         arr[i] = char;
      };
   };
   return arr;
};
console.log(changeVowel(arr));

Output

[ 5, 23, 67, 'e', 56, 'o' ]

Updated on: 17-Apr-2021

170 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements