gpt4 book ai didi

javascript - NaN 问题 - 为什么相同的代码会返回不同的结果?

转载 作者:行者123 更新时间:2023-11-30 08:27:01 24 4
gpt4 key购买 nike

我正在编写一个脚本来将加扰的单词与未加扰的单词进行匹配,但我遇到了一个奇怪的 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>

输出:

  • Word[0]: html:) - 值:NaN
  • 字[1]:121212 - 值:297
    ...省略列表的其余部分...
  • typeof wordVals[0]?: 数字
  • isInteger wordVals[0]?: false
  • Scram Word[0]:leho!l - 值:565
  • 紧急填词[1]:lilwie - 值:646
    ...省略列表的其余部分...

我尝试在我的单词列表中使用不同的起始单词,但得到相同的 NaN 结果。

我不明白为什么第一段代码不起作用,而实际上它与第二段代码确实有效!

为什么我在第一个单词的每个字母的 ASCII 值上得到一个 NaN?

我是初学者,意识到这不是查找单词匹配的正确方法。正则表达式可能是我最终会尝试使用的,但我需要弄清楚这一点,这样我才能在我学习的方式上取得进步。

谢谢。我一直使用这个网站,因为我喜欢你们大多数人在处理用户请求时所采用的严肃方法。这里有很多非常聪明的人贡献了他们的时间和智慧,我真的很感激。

我也乐于接受您给我的任何改进此代码的提示。

最佳答案

那是因为undefined + NumberNaN。最初,sum 变量是未定义。这就是为什么 wordVals 数组中的第一个值是 NaN

只需将 sum 设置为 0,它就会按预期工作,例如:

var words, scram, wordVals, scramVals, sum = 0, i, I;

关于javascript - NaN 问题 - 为什么相同的代码会返回不同的结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44039744/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com