私はJSの初心者ですが、乗算や加算などの結果が20より大きい場合にアラートを作成する計算機にif文を作成しようとしました電卓は正常に動作しますが、 'if'ステートメントは正しく動作しません。シンプルなJavaScriptの計算機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");
}
コードはあなたの例に従って実行されていません。コールバックを作成してそこに条件を配置することができます。または、これらの関数をどのように呼び出すかのコードを表示してください。 –
@jkrisのメソッドを使用する – Dherya