Program to make vowels in string uppercase and change letters to next letter in alphabet(i.e. a->b) not working

StackOverflow https://stackoverflow.com/questions/21143554

Question

how can I solve this. It is supposed to make all vowels in str capitalized and to change each letter in the alphabet to the next letter in the alphabet(i.e. a -> b, or z->a). It keeps returning "str.join is not a function". Any Help? By the way, it's JavaScript.

function LetterChanges(str) { 

  str = str.split("");//split() string into array
  for(var i=0;i<str.length;str++){//for loop that checks each letter
    if(str[i]===/[^a-y]/){
      str=str[i].fromCharCode(str[i].charCodeAt(0) + 1);
        }else if(str[i]==='z'){
          str[i] = 'a';
        }
    if(str[i] === 'a'||'e'||'i'||'o'||'u'){
       str[i] = str[i].toUpperCase();
       }

  }

  //modifies letter by adding up in alphabet
  //capitalizes each vowel
  //join() string


  return str.join(); 
}

// keep this function call here 
// to see how to enter arguments in JavaScript scroll down
LetterChanges(readline());                            
Was it helpful?

Solution

Ok, you need to read a bit on JavaScript.

This is doesn't do what you think str[i]===/[^a-y]/. You should use str[i].match(/[a-y]/i).

This doesn't do what you think: str[i] === 'a'||'e'||'i'||'o'||'u'. It'll always return true. You'll want this str[i].match(/[aeiou]/i).

OTHER TIPS

There are several logical errors in your code, as described in @Simon Boudrias answer.

However just for self-learning here is an alternative solution for your problem:

str.replace(/[a-z]/g, function(c) {
    return 'aeiou'.indexOf(c) > -1
        ? c.toUpperCase()
        : String.fromCharCode(Math.max(c.charCodeAt(0) % 122 + 1, 97));
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top