我正在编写一个脚本来将加扰的单词与其未加扰的对应词匹配,我遇到了一个奇怪的 NaN 问题。我在下面插入了评论,以指导您进一步了解我的问题,希望您能给我解释为什么会出现这个问题。感谢您的时间和帮助。
注意:完全相同(但变量名不同)的两段代码行为不同。后者有效,但前者无效。
<!DOCTYPE html>
<html>
<head>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<meta content="utf-8" http-equiv="encoding">
</head>
<body>
<form id="words">
<input type="hidden" value="html:),121212,21122112,asdfjkl;,hal9000,1234qwer,1q2w3e,test123,gambit,sports,hello!,willie,hashtags,oneway,whoknows,whyowhy,youwho,theone,sweet!,wo0Oot">
</form>
<br>
<li>leho!l</li>
<li>lilwie</li>
<li>thahsags</li>
<li>yaneow</li>
<li>kwosonhw</li>
<li>ohhywwy</li>
<li>oyuwoh</li>
<li>otheen</li>
<li>e!stwe</li>
<li>wtoO0o</li>
<script>
var words, scram, wordVals, scramVals, sum, i, I;
// Turn word list into an array.
words = [];
var words = document.getElementById("words").elements[0].value.split(",");
// Turn scrambled word list into an array.
scram = [];
for (var i = 0; i < 10; i++) {
scram.push(document.getElementsByTagName("li")[i].innerHTML);
};
// Next I iterate through each letter of each word (for the unscrambled words)
// and convert those letters into ASCII values - adding those together
// to get the total ASCII value of each word.
wordVals = [];
for (i = 0; i < words.length; i++) {
for (I = 0; I < words[i].length; I++) {
sum += words[i].charCodeAt(I);
// console.log(sum)
// Using console.log(sum), you see that the first 6 iterations return NaN.
// Each iteration (ASCII Value) for the first word in this loop
// words[0].charCodeAt(0, 1, 2, 3, 4, 5, 6), all returned NaN.
// But each word after the first iteration calculates the ASCII values
// like it's supposed to.
};
wordVals.push(parseInt(sum));
sum = 0;
console.log("Word[" + i + "]: " + words[i] + " - Value: " + wordVals[i]);
};
// typeof WordVals[0] = number
console.log("typeof wordVals[0]?: " + typeof(wordVals[0]));
// isInteger = false
console.log("isInteger wordVals[0]?: " + Number.isInteger(wordVals[0]));
// Here I am iterating through each letter of each word (for the scrambled words)
// and converting those letters into ASCII values - adding those values together
// to get the total ASCII value of each word.
// **This loop uses the same exact code as the one above (but with different variables)
// and it works correctly. Why is this?
scramVals = [];
for (i = 0; i < scram.length; i++) {
for (I = 0; I < scram[i].length; I++) {
sum += scram[i].charCodeAt(I);
};
scramVals.push(sum);
sum = 0;
console.log("Scram Word[" + i + "]: " + scram[i] + " - Value: " + scramVals[i]);
};
</script>
</body>
</html>
输出:
- 字[0]:html :) - 值:NaN
- Word[1]:121212 - 值:297
...省略列表的其余部分... - typeof wordVals[0]?: 数字
- isInteger wordVals[0]?: false
- Scram Word[0]:leho!l - 值:565
- Scram Word[1]:lilwie - 值:646
...省略列表的其余部分...
我尝试在我的单词列表中使用不同的起始单词,但得到相同的 NaN 结果。
我不明白为什么第一个代码块实际上与第二个有效的代码块相同时不起作用!
为什么我在第一个单词的每个字母的 ASCII 值上得到一个 NaN?
我是一个初学者,并意识到这不是寻找单词匹配的正确方法。正则表达式可能是我最终会尝试使用的,但我需要弄清楚这一点,这样我才能在学习的方式上取得进步。
谢谢你。我一直使用这个网站,因为我喜欢你们大多数人在处理用户请求时采取的严肃方法。这里有很多非常聪明的人贡献他们的时间和智慧,我真的很感激。
我也愿意接受您可以给我的任何提示来改进此代码。