1

I have just fiddled around with javascript, I'm just trying to compare 2 arrays and remove all the english vowels from the string to later return it with no vowels.

The iteration for j representing the array of vowels (arr) stops at length but I want it to re-loop on every iteration for i.

Here is my basic code:

let sentence = 'eitan was here';

function disemvowel(str) {
  let arr = ['a', 'e', 'i', 'o', 'u'];
  let letters = str.split('');
  let i, j;
  for (i = 0, j = 0; i < letters.length && j < arr.length;) {
    console.log('counter: i = ' + i + ', j = ' + j + ', ' + letters[i] + ' vs ' + arr[j] + 'and letters: ' + letters);
    if (letters[i] == arr[j]) {
      if (letters[i] == ' ') {
        i++;
      }
      console.log('IF stopped here: ' + letters[i] + ' at i: ' + i + ', ' + arr[j] + ' at j: ' + j);
      letters.splice(i, 1);
      //console.log('letters after splice: '+ letters);
      i++;
    } else {
      console.log('ELSE stopped here: ' + letters[i] + ' at i: ' + i + ', ' + arr[j] + ' at j: ' + j);
      j++;
    }
  }
  return letters;

}
console.log('letters are: ', disemvowel(sentence));
I'm interested to avoid creating loops within loops or separate functions to run j for runtime and brevity.

9
  • 1
    "I'm interested to avoid creating loops within loops" Why, they are a powerful tool. Commented Jun 29, 2017 at 7:15
  • @Teemu , true but I have this tingling feeling that it wouldn't be necessary here, am I right? Commented Jun 29, 2017 at 7:17
  • 1
    Check my answer. This is what you exactly needed. Commented Jun 29, 2017 at 7:20
  • 1
    Basicly, if you're going to check an array against a string, you have to always create a loop. Whether it would be internal or external is up to you. Commented Jun 29, 2017 at 7:21
  • 1
    @clusterBuddy It does not matter if you do it in single run or in loop within loop your runtime will be linear, since one loop (vowel one) will always have a constant runtime. Micro optimisations are unnecessary here, you would not gain any significant performance improvement. Commented Jun 29, 2017 at 7:24

3 Answers 3

3

You do not need to loop over to remove the vowels from the string. The best way to achieve this is to use character class in replace() like this

let sentence = 'eitan was here';
sentence  = sentence .replace(/[aeiou]/g,'');

You can also use the OR operator (|) like this

sentence = sentence.replace(/a|e|i|o|u/g,''); 
Sign up to request clarification or add additional context in comments.

4 Comments

awesome answer but I need to stick to the for loop.
Could you please mark it and up vote it so that others can know it is a proper answer.
I voted it up because it's a nice answer but it doesn't solve the 1 loop issue, I would want j to re-loop on every i iteration.
Ok let me look over to that as well.
1
sentence = 'eitan was here';
function disemvowel(str){
arr = ['a','e','i','o','u'];

arr.forEach((itm)=>{
    var re = new RegExp(itm,'g');
    str = str.replace(re,'');
})
return str;
}
console.log(disemvowel(sentence));

1 Comment

thanks , hope it solves your problem of using one loop
0

You have to reset j to 0. Also, You can not splice the same array that you are looping through i.e letters array in this case.

<script>
 let sentence = 'eitan was here';
 function disemvowel(str) {
  let arr = ['a', 'e', 'i', 'o', 'u'];
  let letters = str.split('');
  let output = [];
  let i,j;
 for (i=0,j=0; i<letters.length && j<arr.length;){
    if(letters[i] == arr[j]){
        j = 0;
        i++;
    }else{
        j++;
        if(j >= arr.length){
            output.push(letters[i]);
            i++;
            j = 0;
        }
     }
   }
  return output;
 }
 console.log('letters are: ', disemvowel(sentence));
</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.