【发布时间】:2021-08-11 04:30:05
【问题描述】:
我目前在 codewars 网站上尝试改进我的 JavaScript 以及其他方面。这个问题会给出这样的输入
'is2 Thi1s T4est 3a' 并期待像这样的输出 'Thi1s is2 3a T4est'
我在索引字符串时遇到问题。这似乎很奇怪,因为我以前在 Python 中做过,但即使在 Python 中尝试,我也没有得到正确的输出。
这是我的代码,之前它至少给了我一个数字数组,但现在我只得到[ Array(4) ]。
我知道正确的输出应该是一个字符串,但是一旦我把它全部放在一个数组中,我打算只 tempSentence.join(" ") 数组并返回它。抱歉,这有点乱,但我一直在尝试不同的东西:
function order(words){
let hasNumber = /\d/;
let newSentence = words.split(" ")
let tempSentence = words.split(" ")
for (let i = 0; i < newSentence.length; i++){
for (let j = 0; j < newSentence[i].length; j++){
if (hasNumber.test(newSentence[i][j])){
tempSentence[words[i][j]-1] = newSentence[i]
}
}
}
return tempSentence
}
【问题讨论】:
标签: javascript arrays indexing