Encrypting censored words using JavaScript


Problem

We are required to write a JavaScript function that takes in a string. Our function should convert the string according to following rules −

The words should be Caps, Every word should end with '!!!!', Any letter 'a' or 'A' should become '@', Any other vowel should become '*'.

Example

Following is the code −

 Live Demo

const str = 'ban censored words';
const maskWords = (str = '') => {
   let arr=str.split(' ');
   const res=[]
   for (let i=0; i<arr.length; ++i){
      let s=(arr[i].toUpperCase()+'!!!!').split('');
      for (let j=0; j<s.length; ++j){
         if (s[j]=='A')
            s[j]='@';
         if (s[j]=='E'||s[j]=='I'||s[j]=='O'||s[j]=='U')
         s[j]='*';
      }
      res.push(s.join(''));
   };
   return res.join(' ')
};
console.log(maskWords(str));

Output

B@N!!!! C*NS*R*D!!!! W*RDS!!!!

Updated on: 17-Apr-2021

190 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements