加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
pig_latin.js 558 Bytes
一键复制 编辑 原始数据 按行查看 历史
bostonhsu 提交于 2023-10-02 14:28 . Learn algorithm from khan acdemecy
// Returns true if the given character is an English vowel
function isVowel(char) {
var vowels = ['a', 'e', 'i', 'o', 'u'];
return vowels.indexOf(char.toLowerCase()) !== -1;
}
// Pig Latin algorithm
var words = ["peanut", "butter", "and", "jelly"];
for (var i = 0; i < words.length; i++) {
var word = words[i];
word += "-";
var firstLetter = word.charAt(0);
if (isVowel(firstLetter)) {
word += "yay";
} else {
word += firstLetter;
word += "ay";
word = words.slice(1);
}
words[i] = word;
}
// Display magnificent result
console.log(words);
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化