如何在charCodeAt中记录多个字符

时间:2016-09-30 02:20:55

标签: javascript

对于下面的代码,它显然只会记录" m"在“" man"因为" 0"。如何记录所有三个字母?

var userChoice = prompt("let me encode something!");
var userChoice = (userChoice.charCodeAt(0) );
console.log (userChoice);

2 个答案:

答案 0 :(得分:0)

使用普通循环(向后兼容旧版浏览器)循环遍历userChoice

中的所有字符
var userChoice = prompt("let me encode something!");
for (var i = 0; i < userChoice.length; i++) {
  console.log (userChoice.charCodeAt(i));
}

答案 1 :(得分:0)

怎么回事?

var userChoice = prompt("let me encode something!");
for (i = 0; i < userChoice.length; i++) {
    console.log(userChoice.charAt(i));
}


var userChoice = prompt("let me encode something!");
for (i = 0; i < userChoice.length; i++) {
    console.log(userChoice.charCodeAt(i));
}