mercredi 12 août 2020

string alternates between vowel and consonent

  1. I am supposed to create a function that accepts a string as an argument and validates whether the vowels and consonants are in alternate order throughout. It should return true or false accordingly.
  2. I tried this below, but it only comes out as true and does not check for the other possibilities of true and false. What am i missing/doing wrong? I dont want just the answer, I would also appreciate an explanation of what is going on in this function to set up how it should be properly.
function isAlt(str) {
  let vowels = ["a", "e", "o", "i", "u"];
    
  for(i = 0; i < str.length; i++) {
    if(vowels.indexOf(str[i]) >= 0) {
      if(vowels.indexOf(str[i+1]) === -1);
        return true;
    } else if(vowels.indexOf(str[i]) ===-1) {
      if(vowels.indexOf(str[i+1]) >= 0)
        return true; 
    } else if(vowels.indexOf(str[i]) >= 0) {
      if(vowels.indexOf(str[i+1]) >=0);
        return false;
    } else if(vowels.indexOf(str[i]) === -1) {
      if(vowels.indexOf(str[i+1]) === -1);
        return false; 
    }
  }
}

isAlt('apple'); // apple is false, amazon is true.

Aucun commentaire:

Enregistrer un commentaire