Replace all consonants with nearest vowels in a string | Function to check if a character is vowel or not ; Function to replace consonant with nearest vowels ; if , string element is vowel , jump to next element ; check if consonant lies between two vowels , if it lies , than replace it with nearest vowel ; here the bsolute difference of ascii value is considered ; when s [ i ] is equal to either ' v ' , ' w ' , ' x ' , ' y ' , ' z ' ; Driver code
function isVowel ( ch ) { if ( ch !== " " && ch !== " " && ch !== " " && ch !== " " && ch !== " " ) return false ; return true ; } function replacingConsonants ( s ) { for ( var i = 0 ; i < s . length ; i ++ ) { if ( isVowel ( s [ i ] ) ) continue ; else { if ( s [ i ] > " " && s [ i ] < " " ) { if ( Math . abs ( s [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ) > Math . abs ( s [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ) ) s = s . substring ( 0 , i ) + " " + s . substring ( i + 1 ) ; else s = s . substring ( 0 , i ) + " " + s . substring ( i + 1 ) ; } else if ( s [ i ] > " " && s [ i ] < " " ) { if ( Math . abs ( s [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ) > Math . abs ( s [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ) ) s = s . substring ( 0 , i ) + " " + s . substring ( i + 1 ) ; else s = s . substring ( 0 , i ) + " " + s . substring ( i + 1 ) ; } else if ( s [ i ] > " " && s [ i ] < " " ) { if ( Math . abs ( s [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ) > Math . abs ( s [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ) ) s = s . substring ( 0 , i ) + " " + s . substring ( i + 1 ) ; else s = s . substring ( 0 , i ) + " " + s . substring ( i + 1 ) ; } else if ( s [ i ] > " " && s [ i ] < " " ) { if ( Math . abs ( s [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ) > Math . abs ( s [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ) ) s = s . substring ( 0 , i ) + " " + s . substring ( i + 1 ) ; else s = s . substring ( 0 , i ) + " " + s . substring ( i + 1 ) ; } else if ( s [ i ] > " " ) s = s . substring ( 0 , i ) + " " + s . substring ( i + 1 ) ; } } return s ; } var s = " " ; document . write ( replacingConsonants ( s ) ) ;