Checking for vowels in array of numbers using JavaScript

JavascriptWeb DevelopmentFront End Technology

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' ]
raja
Published on 17-Apr-2021 16:42:54
Advertisements