gpt4 book ai didi

javascript - 简单的 JavaScript 计算器 if 语句

转载 作者:行者123 更新时间:2023-11-28 05:17:37 25 4
gpt4 key购买 nike

我是 JS 新手,但我尝试在计算器中创建一个“if”语句,当乘法或加法等的结果大于 20 时,它会发出警报。计算器工作正常但“if”语句则不然。

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");
}

关于javascript - 简单的 JavaScript 计算器 if 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40841474/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com