mardi 2 juin 2020

Reversing vowels in string in javaScript

This is my code for reversing vowel in a string. I couldnt find what mistake i have done in this.. example: ip: mobile op: mebilo

but.. for me..whatever Input i gave, its only being displayed as output. whats my mistake? Thank you.

const readline = require('readline');
const inp = readline.createInterface({
  input: process.stdin
});
const userInput = [];
  inp.on("line", (data) => {
  userInput.push(data);
});
inp.on("close", () => {
  a= userInput[0];
  b=[];
  n=0;
  for(i=0; i<a.length; i++){
    if(a[i]=="a"||a[i]=="e"||a[i]=="i"||a[i]=="o"||a[i]=="u"){
      b[n]=a[i];
      n+=1;
    }
  }

  last = b.length-1;
  for(j=0; j<a.length; j++){
    if(a[j]=="a"||a[j]=="e"||a[j]=="i"||a[j]=="o"||a[j]=="u"){
      a[j]=b[last];
      last-=1;
    }
  } 
  console.log(a);  
})

Aucun commentaire:

Enregistrer un commentaire