我目前使用hint()具有这个超级基本的“计算器”:
var numberOne = parseInt(prompt("Enter a number"))
var numberTwo = parseInt(prompt("Enter another number"))
let additionAnswer = numberOne + numberTwo
console.log("Addition answer = " + additionAnswer);
let substractionAnswer = numberOne - numberTwo
console.log("Substraction answer is " + substractionAnswer);
let multiplicationAnswer = numberOne * numberTwo
console.log("Multiplication answer = " + multiplicationAnswer);
let divisionAnswer = numberOne / numberTwo
console.log("Division answer is " + divisionAnswer);
现在我只是想知道我该怎么做才能验证提示中写的确实是数字。
答案 0 :(得分:0)
继续提示直到收到号码?
var numberOne = '';
var numberTwo = '';
while (isNaN(parseFloat(numberOne)) || !isFinite(numberOne)) {
numberOne = prompt("Enter a number");
}
while (isNaN(parseFloat(numberTwo)) || !isFinite(numberTwo)) {
numberTwo = prompt("Enter a number");
}
numberOne = parseInt(numberOne);
numberTwo = parseInt(numberTwo);
let additionAnswer = numberOne + numberTwo
console.log("Addition answer = "+ additionAnswer);
let substractionAnswer = numberOne - numberTwo
console.log("Substraction answer is "+ substractionAnswer);
let multiplicationAnswer = numberOne * numberTwo
console.log("Multiplication answer = "+ multiplicationAnswer);
let divisionAnswer = numberOne / numberTwo
console.log("Division answer is "+ divisionAnswer);
答案 1 :(得分:0)
您可以使用('/\d/').test(str1)
。