【发布时间】:2019-06-07 16:22:59
【问题描述】:
我有以下几点:
function showUnicode()
{
var text = prompt( 'Enter the wanted text', 'Unicode' ),
unicode = 0,
ntext,
temp,
i = 0
;
// got the text now transform it in unicode
for(i; i < text.length; i++)
{
unicode += text.charCodeAt(i)
}
// now do an alert
alert( 'Here is the unicode:\n' + unicode + '\nof:\n' + text )
}
感谢初始化 unicode 的想法,但现在 unicode 变量获取最后一个字符的 Unicode,为什么会这样?
【问题讨论】:
-
unicode没有初始化,所以是undefined。在第一次迭代中,你基本上是在做undefined + someNumber和undefinedis converted toNaN。 -
charCodeAt 返回一个表示 unicode 代码点值的整数。如果按原样将它们相加,您将得到“1+2+3=6”的等价物,而不是“123”。
-
无需屏蔽您的 JavaScript 块:
-
一个好的做法 - 用分号结束每个语句。
-
现在我将 unicode 初始化为 0,但它只显示最后一个字符的 unicode
标签: javascript unicode