[英]Simple javascript calculator if statement
我是JS的新手,但我試圖在我的計算器中創建一個'if'語句,當乘法或加法等的結果大於20時會產生警告。計算器工作正常但是'如果'聲明沒有。
JS:
var a,b;
function setValues()
{
a = Number(document.getElementById("a").value);
b = Number(document.getElementById("b").value);
}
function sum()
{
setValues();
result = a+b;
alert("the sum is equal to "+result);
}
function rest()
{
setValues();
result = a-b;
alert("the rest is equal to "+result);
}
function mult()
{
setValues();
result = a*b;
alert("the operation is equal to "+result);
}
function div()
{
setValues();
result = a/b;
alert("the operation is equal to "+result);
}
if (result > 20) {
alert("heyy thats pretty big");
}
你的if
語句應該在每個運算符函數中,如下所示:
function div()
{
setValues();
result = a/b;
alert("the operation is equal to "+result);
checkResult(result);
}
function checkResult(result){
if(result > 20)
alert("heyy thats pretty big");
}
問問自己'if'什么時候會被執行。 它不在任何函數內部,所以如何調用它? 如果沒有看到整個頁面,我無法分辨,但看起來它只會在頁面加載時執行。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.