0

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());                            

2 Answers2

1

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).

Simon Boudrias
  • 42,953
  • 16
  • 99
  • 134
  • Thanks. This helps a lot. And yes, I'm trying to teach myself Javascript. Does === not work when comparing strings? Do I always have to use .match() when comparing strings? – Colin Michael Flaherty Jan 15 '14 at 17:14
  • @user3196340 String.match() finds the matches in strings against the passed regular expression. === does work when comparing strings, but it compares for equality without type coercion, rather than matching a regex. – Eric Alberson Jan 15 '14 at 17:19
1

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));
});
Community
  • 1
  • 1
VisioN
  • 143,310
  • 32
  • 282
  • 281