javascript vowels to uppercase and consonants to lowercase

The question I’m doing is:

Write a JS program which takes 1 input from the user: a sentence (string). The program contains a function which converts the vowels (a,e,i,o,u) in the string to uppercase and consonants (non-vowel characters) into lowercase. Sample Input: string = “Learn programming and start earning”. Expected Output = “lEArn prOgrAmmIng And stArt EArnIng”. Your output code should be in the format console.log(“Result is “, Output variable).

And my code is:

var sentence=prompt("Enter the sentence");

function convert(x){
 var a = x.split('');
 for( var i=0 ; i<a.length ; i++){
    if(a[i]=="a"){
        a[i] = a[i].toUpperCase();
    }
    else if(a[i]=="e"){
        a[i] = a[i].toUpperCase();
    }
    else if(a[i]=="i"){
        a[i] = a[i].toUpperCase();
    }
    else if(a[i]=="o"){
        a[i] = a[i].toUpperCase();
    }
    else if(a[i]=="u"){
        a[i] = a[i].toUpperCase();
    }
    else{
        a[i] = a[i].toLowerCase();
    }
  }
  var b = a.join('');
  return b;
 }

var output = convert(sentence);
console.log("Result is ", output);

It doesn’t look good but provides output. However, it does not pass all test cases according to the webapp. What am I doing wrong?

46 thoughts on “javascript vowels to uppercase and consonants to lowercase”

Leave a Comment