题目如下:
Have the function LetterChanges(str) take the str parameter being passed and modify it using the following algorithm. Replace every letter in the string with the letter following it in the alphabet (ie. c becomes d, z becomes a). Then capitalize every vowel in this new string (a, e, i, o, u) and finally return this modified string.

例子:
输入: Asdfghjg hjk=+=3 输出: BtEghIkh Ikl=+=3

输入: shf sAfkun trteuFgitn 输出: tIg tBglvO UsUfvGhjUO

答案:

  1. function LetterChanges(str) {
  2. // code goes here
  3. var a = str.replace(/[a-zA-Z]/g, function(ch) {
  4. if (ch === 'z') return 'a'
  5. if (ch === 'Z') return 'A'
  6. return String.fromCharCode(ch.charCodeAt(0) + 1)
  7. })
  8. return a.replace(/[aeiou]/g, function(ch) {
  9. return ch.toUpperCase()
  10. })
  11. }

三个知识点:

String.fromCharCode :

静态 String.fromCharCode() 方法返回使用指定的Unicode值序列创建的字符串。链接

charCodeAt:

**charCodeAt()** 方法返回0到65535之间的整数,表示给定索引处的UTF-16代码单元 (在 Unicode 编码单元表示一个单一的 UTF-16 编码单元的情况下,UTF-16 编码单元匹配 Unicode 编码单元。但在——例如 Unicode 编码单元 > 0x10000 的这种——不能被一个 UTF-16 编码单元单独表示的情况下,只能匹配 Unicode 代理对的第一个编码单元) 。如果你想要整个代码点的值,使用 codePointAt()。 链接
语法:

  1. str.charCodeAt(index)
  2. // 例如: 'hello'.charCodeAt(0) // 72

toLocaleUpperCase 与 toUpperCase的区别:

都是转化为大写, 与 toUpperCase() 不同的是,toLocaleUpperCase() 方法按照本地方式把字符串转换为大写。只有几种语言(如土耳其语)具有地方特有的大小写映射,所有该方法的返回值通常与 toUpperCase() 一样。