【发布时间】:2011-05-14 08:22:52
【问题描述】:
对于这个新手问题我很抱歉,但这让我发疯了。
我有话要说。对于单词的每个字母,找到一个数组中的字符位置,然后返回在并行数组中找到的相同位置的字符(基本密码)。这是我已经拥有的:
*array 1 is the array to search through*
*array 2 is the array to match the index positions*
var character
var position
var newWord
for(var position=0; position < array1.length; position = position +1)
{
character = array1.charAt(count); *finds each characters positions*
position= array1.indexOf(character); *index position of each character from the 1st array*
newWord = array2[position]; *returns matching characters from 2nd array*
}
document.write(othertext + newWord); *returns new string*
我遇到的问题是,目前该函数只写出新单词的最后一个字母。我确实想在 document.write 中添加更多文本,但如果我放在 for 循环中,它会写出新单词以及每个单词之间的其他文本。我真正想做的是返回 othertext + newWord 而不是 document.write 以便我以后可以使用它。 (只是使用 doc.write 给我的代码发短信):-)
我知道这很简单,但我看不出哪里出错了。有什么建议吗? 谢谢 伊西
【问题讨论】:
-
嗨,Issy,欢迎来到 SO。不幸的是,如果大多数用户听到“家庭作业”式的问题,他们就不太可能做出回应。另外,这是什么语言的?
-
嗨史蒂夫,谢谢你的回复,是的,我知道这很困难,我真的很想自己解决这个问题,但我只需要一个正确的方向,因为我完全坚持去哪儿。它在 JavaScript 中。谢谢伊西
标签: javascript string parallel-arrays