1

i have created an array for vowels position in string now i want reomve all elements that have value -1 from this array but its not working

function translatePigLatin(str) {
  var vowelp=[];
  var newarr=str.split('');
   vowelp.push(newarr.indexOf('a'));
   vowelp.push(newarr.indexOf('e'));
   vowelp.push(newarr.indexOf('i'));
   vowelp.push(newarr.indexOf('o'));
   vowelp.push(newarr.indexOf('u'));
   var minvowel=vowelp[0];
for(var i=0;i<vowelp.length;i++) {   //looping through vowel's position array
    if(vowelp[i]==-1)  {
   vowelp.splice(i,1);
      console.log(vowelp[i]);
      }
  }
  return vowelp;
}

input-translatePigLatin("consonant"); output that i am getting is[6,-1,1] but i want [6,1]

Shubham Shukla
  • 988
  • 2
  • 13
  • 28

3 Answers3

0

Simple way is to use filter()

function translatePigLatin(str) {
  var vowelp = [];
  var newarr = str.split('');
  vowelp.push(newarr.indexOf('a'));
  vowelp.push(newarr.indexOf('e'));
  vowelp.push(newarr.indexOf('i'));
  vowelp.push(newarr.indexOf('o'));
  vowelp.push(newarr.indexOf('u'));
  var minvowel = vowelp[0];
  return vowelp.filter(function(v) {
    return v != -1;
  })
}

console.log(translatePigLatin("consonant"));

In your case you need to decrement the value of i in case of item removal otherwise it will skip the next element.

function translatePigLatin(str) {
  var vowelp = [];
  var newarr = str.split('');
  vowelp.push(newarr.indexOf('a'));
  vowelp.push(newarr.indexOf('e'));
  vowelp.push(newarr.indexOf('i'));
  vowelp.push(newarr.indexOf('o'));
  vowelp.push(newarr.indexOf('u'));
  var minvowel = vowelp[0];
  for (var i = 0; i < vowelp.length; i++) { //looping through vowel's position array
    if (vowelp[i] == -1) {
      vowelp.splice(i, 1);
      i--;
      console.log(vowelp[i]);
    }
  }
  return vowelp;
}

console.log(translatePigLatin("consonant"));

You can make it more simple using map() and filter() with an array

function translatePigLatin(str) {
  return ['a', 'e', 'i', 'o', 'u'].map(function(v) {
    return str.indexOf(v);
  }).filter(function(v) {
    return v != -1;
  });
}

console.log(translatePigLatin("consonant"));
Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188
0

you are calling splice on the same array you are iterating over. Rememeber splice is mutable and it deletes from the original array. As a result of that your index tracking logic is getting messed up. So instead you could use delete[i] (which does not mess up the indexes and creates a void)

 function translatePigLatin(str) {
  var vowelp=[];
  var newarr=str.split('');
  vowelp.push(newarr.indexOf('a'));
  vowelp.push(newarr.indexOf('e'));
  vowelp.push(newarr.indexOf('i'));
  vowelp.push(newarr.indexOf('o'));
  vowelp.push(newarr.indexOf('u'));
  var minvowel=vowelp[0];
  for(var i=0;i<vowelp.length;i++) {   //looping through vowel's position array
    if(vowelp[i]==-1)  {
        delete vowelp[i];
    }
  }
  return vowelp;
}


  console.log(translatePigLatin("consonant")); //prints [6, 3: 1]  

which means you have 6 at index 0 and 1 at index 3

Wild Widow
  • 2,359
  • 3
  • 22
  • 34
0

I would prefer a simpler code:

function translatePigLatin(str) {
  var vowelp = [];
  var vowels = ['a','e','i','o','u'];
  for (var i = 0; i < vowels.length; i++) {
    var index = str.indexOf(vowels[i]);
    if (index != -1) {
        vowelp.push(index);
    }
  }
  return vowelp;
}
Daniel Barral
  • 3,896
  • 2
  • 35
  • 47