这是我到目前为止使用的JavaScript。它在任何文本框中都给了我一个错误,我输入了除整数以外的任何内容,但仍然计算了其他数字。
function finalGrade(number) {
//set a default value for this just to create the variable
var numberOrder = '';
//test the number and create a message string to return
if (number >= 90.00) {
numberOrder = 'A';
} else if (number <= 89.99) {
numberOrder = 'B';
} else if (number <=79.99){
numberOrder = 'C';
}
else if (number <=69.99){
numberOrder = 'D';
}
else if (number <=59.99){
numberOrder = 'F';
}
return numberOrder;
}
function calculateNumbers() {
//this will get the text from the text box
var number1 = document.getElementById('text-box-1').value;
/* this will print to the console
this is for debugging, not for the end user */
console.log("The text in text-box-1 is: " + number1);
/* these next two lines turn will do the same as
the previous two lines for box 1*/
var number2 = document.getElementById('text-box-2').value;
console.log("The text in text-box-2 is: " + number2);
//this is text box 3
var number3 = document.getElementById('text-box-3').value;
console.log("The text in text-box-3 is: " + number3);
//this is text box 4
var number4 = document.getElementById('text-box-4').value;
console.log("The text in text-box-4 is: " + number4);
//this is text box 5
var number5 = document.getElementById('text-box-5').value;
console.log("The text in text-box-5 is: " + number5);
//if the STRING number1 does not contain only digits
if (!stringIsNumbers(number1)) {
//set and error message
var message = "test score 1 is not a whole, positive
integer";
//display the error in the console and to the page
console.log(message);
document.getElementById('answerLabel').innerHTML = message;
} else if (!stringIsNumbers(number2)) {
// testing the STRING number2 the same way
var message = "test score 2 is not a whole, positive
integer";
console.log(message);
document.getElementById('answerLabel').innerHTML = message;
}
else if (!stringIsNumbers(number3)) {
// testing the STRING number3 the same way
var message = "homework average is not a whole, positive
integer";
console.log(message);
document.getElementById('answerLabel').innerHTML = message;
}
else if (!stringIsNumbers(number4)) {
// testing the STRING number4 the same way
var message = "in-class average is not a whole, positive
integer";
console.log(message);
document.getElementById('answerLabel').innerHTML = message;
}
else if (!stringIsNumbers(number5)) {
// testing the STRING number5 the same way
var message = "final project is not a whole, positive
integer";
console.log(message);
document.getElementById('answerLabel').innerHTML = message;
}
else {
//passed both tests, so now I can process the numbers
/*these next four lines turn the text into numbers,
but this won't work if the text does not represent a number*/
number1 = parseInt(number1);
number2 = parseInt(number2);
number3 = parseInt(number3);
number4 = parseInt(number4);
number5 = parseInt(number5);
var grade1 = (number1*0.15);
var grade2 = (number2*0.15);
var grade3 = (number3*0.40);
var grade4 = (number4*0.20);
var grade5 = (number5*0.10);
//do the math and show it in the console
result = grade1 + grade2 + grade3 + grade4 + grade5;
console.log("The answer is: " + result.toFixed(2));
//put the answer back into the Web page
document.getElementById('answer').innerHTML =
result.toFixed(2);
}
}
</script>
我希望在按下计算后出现错误,就好像用户输入了一个负整数,一个单词或短语,或者除了正整数以外的任何其他东西一样。
答案 0 :(得分:0)
这应该剪掉它。我已经稍微更新了您的代码,使其更加现代。另外,我添加了功能checkNumber
,以检查是否应添加错误消息。
checkNumber = num => isNaN(num) || Number(num) <= 0;
finalGrade = number => {
if (number >= 90.00) {
return 'A';
} else if (number <= 89.99) {
return 'B';
} else if (number <= 79.99) {
return 'C';
} else if (number <= 69.99) {
return 'D';
} else if (number <= 59.99) {
return 'F';
}
}
calculateNumbers = () => {
let number1 = document.getElementById('text-box-1').innerText;
let number2 = document.getElementById('text-box-2').innerText;
let number3 = document.getElementById('text-box-3').innerText;
let number4 = document.getElementById('text-box-4').innerText;
let number5 = document.getElementById('text-box-5').innerText;
if (checkNumber(number1)) {
//set and error message
let message = "test score 1 is not a whole, positive integer ";
document.getElementById('answerLabel').innerText = message;
} else if (checkNumber(number2)) {
let message = "test score 2 is not a whole, positive integer ";
document.getElementById('answerLabel').innerText = message;
} else if (checkNumber(number3)) {
let message = "homework average is not a whole, positive integer ";
document.getElementById('answerLabel').innerText = message;
} else if (checkNumber(number4)) {
let message = "in-class average is not a whole, positive integer ";
document.getElementById('answerLabel').innerText = message;
} else if (checkNumber(number5)) {
let message = "final project is not a whole, positive integer ";
document.getElementById('answerLabel').innerText = message;
} else {
number1 = parseInt(number1);
number2 = parseInt(number2);
number3 = parseInt(number3);
number4 = parseInt(number4);
number5 = parseInt(number5);
let grade1 = (number1 * 0.15);
let grade2 = (number2 * 0.15);
let grade3 = (number3 * 0.40);
let grade4 = (number4 * 0.20);
let grade5 = (number5 * 0.10);
result = grade1 + grade2 + grade3 + grade4 + grade5;
document.getElementById('answer').innerText = `${finalGrade(result)} (${result.toFixed(2)}/100)`;
}
}
calculateNumbers();
<div id="text-box-1">1</div>
<div id="text-box-2">2</div>
<div id="text-box-3">3</div>
<div id="text-box-4">4</div>
<div id="text-box-5">5</div>
<br/> Result:
<span id="answerLabel">successful</span>
<br/> Answer:
<span id="answer"></span>
希望这会有所帮助,