我对这个问题有两个疑问。我正在用 JavaScript 编写 PigLatin 代码,其中以下单词应该返回以下响应:
"computer" == "omputercay"
"think" == "inkthay"
"algorithm" == "algorithmway"
"office" == "officeway"
"Computer" == "Omputercay"
"Science!" == "Iencescay!"
但是,我的代码为最后两个词返回以下响应: “omputerCay”和“ience!Scay”。我的第一个查询是找出如何将 omputerCay 的第一个字母大写并使“C”小写。二是做同样的事情还要动“!”到词尾。
建议的文章没有帮助移动“!”到词尾。
function pigLatin(str) {
let vowels = ['a', 'e', 'i', 'o', 'u'];
let newStr = "";
if (vowels.indexOf(str[0]) > -1) {
newStr = str + "way";
return newStr;
} else {
let firstMatch = str.match(/[aeiou]/g) || 0;
let vowel = str.indexOf(firstMatch[0]);
newStr = str.substring(vowel) + str.substring(0, vowel) + "ay";
return newStr;
}}