- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
到目前为止我已经:
function pigIt(str) {
//split string into array of words
let words = str.split(" ");
//loop through array of words
for (let i = 0; i < words.length; i++) {
//loop through individual words
for (let j = 0; j < words.length; j++) {
//get first word in words
let firstWord = words[0];
//get first character in first word
let firstChar = firstWord[0];
//Create new word without first character
let unshiftedWord = firstWord.unshift(0);
//move first character to the end
let newWord = unshiftedWord.push(firstChar) + "ay";
return newWord;
}
}
}
console.log(pigIt('Pig latin is cool'));
现在,我只想返回“igPay”
。然后,我将把这些字符串组合在一起形成一个新字符串。
但它不喜欢firstWord.unshift(0);
。它说:
TypeError: firstWord.unshift is not a function.
但是.unshift() is a function ?为什么这不起作用?
一旦我得到一个新单词,我应该能够将 newWords
组合在一起形成 newString
,尽管可能有比创建新单词更有效的方法-为每个单独的单词循环。
https://www.codewars.com/kata/520b9d2ad5c005041100000f/train/javascript
编辑:我希望使用传统函数声明而不是箭头符号来编写此函数。
编辑2实现@Ori Drori的代码后,我的函数如下所示:
function pigIt(str) {
newString = str.replace(/(\S)(\S+)/g, '$2$1ay');
return newString;
}
console.log(pigIt('Pig latin is cool'));
它有效 - 但我不明白 str.replace(/(\S)(\S+)/g, '$2$1ay');
到底在做什么。
最佳答案
更简单的方法是使用 map()
和 join()
。
注意:根据 codewars 示例,只有 ay
被添加到包含 aplhabets 的字符串,而不是 !
。因此,您应该使用 test()
测试数组的元素是否为 aplhabet。
以下解决方案通过了 codewars 中的所有测试。
function pigIt(str){
return str.split(' ').map(x =>/[a-zA-Z]+/.test(x) ? x.slice(1)+x[0]+'ay' : x).join(' ');
}
console.log(pigIt('Pig latin is cool'));
function pigIt(str){
return str.split(' ').map(function(x){
return /[a-zA-Z]+/.test(x) ? x.slice(1)+x[0]+'ay' : x;
}).join(' ');
}
console.log(pigIt('Pig latin is cool'));
for
循环这是使用简单的 for
循环的代码
function pigIt(str){
str = str.split(' ');
for(let i = 0;i<str.length;i++){
if(/[a-zA-Z]/.test(str[i])){
str[i] = str[i].slice(1) + str[i][0] + 'ay';
}
}
return str.join(' ');
}
console.log(pigIt('Pig latin is cool'));
关于javascript - 给定一个字符串,将每个单词的第一个字母移动到每个单词的末尾,然后将 "ay"添加到每个单词的末尾并返回一个新字符串 - JavaScript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55501660/
我正在尝试运行一个执行 Pig Latin 的函数。我被困在第一步中。 当我将输入参数设置为“eat”=>时,我希望我的代码返回字符串加上“ay”。请参阅下面的 if 语句代码。 相反,当我运行代码时
vgcreate 命令中的选项“-Ay”“-cy”有什么作用?我查看了手册页,但找不到它们。 例如: vgcreate -Ay -cy cluster_vg /dev/vdb 最佳答案 来自 vgcr
到目前为止我已经: function pigIt(str) { //split string into array of words let words = str.split(" ");
我有一堆 *.ay 文件,其中包含一些元数据(艺术家、标题和“misc”)。我如何将它们转换为 *.flac 以保留该元数据? 最佳答案 http://bulba.untergrund.net/pro
我正在尝试导入 small data set使用 MySQL 的 LOAD DATA 语句获取柏林街道地址。问题是,导入运行后,德国街道名称中所有漂亮的 ß 字符都变成了 à 集。 这是我用于该表的创
我正在尝试使用 GDBus 绑定(bind)通过 D-Bus 生成字节数组。你能告诉我如何实现这一目标吗?我尝试使用谷歌搜索但没有帮助。 字节数组包含图像文件,因此无法转换为 charbytearra
是否有任何库、代码片段或类似的东西可以让我玩 ZX Spectrum我的 XNA Game Studio 游戏中的 .ay 文件? 最佳答案 您应该先将 .ay 文件转换为 wav。有一个程序here
我是一名优秀的程序员,十分优秀!