如何在JavaScript中将字符串转换为整数?
我刚注意到:
//IN CHROME JS CONSOLE
parseInt("03010123"); //prints: 3010123
//IN NODE.JS
parseInt("03010123"); //prints: 790611
Run Code Online (Sandbox Code Playgroud)
由于两者都基于V8,为什么同样的操作会产生不同的结果?
do {
var y = prompt("Enter a positive integer from 1 to 26");
var int = Number.isInteger(y);
var x = parseInt(y);
window.alert(int);
} while (x > 26 || x < 1 || int == false)
Run Code Online (Sandbox Code Playgroud)
这是我编写的代码,但每当我在浏览器上输入整数时,它仍然会警告 int 为 false。
而不是数字添加此代码正在进行字符串连接.如何在提示字段中指定输入的类型(例如:number,string).
var jonage = prompt("enter the johns age");
var jonHeight = prompt("enter the johns height");
var jonScore = jonHeight + jonage * 5;
console.log(jonScore);
Run Code Online (Sandbox Code Playgroud)