2017-06-11 2 views
0

JavaScriptを使用してアルゴリズムをコーディングし、解決しようとしています。私は3つのテキストボックスに3つの数字の入力の最大値を見つけてそれを警告しなければなりません。私はそれをしましたが、テキストボックスが入力なしで空のままになっているときに、いくつかのメッセージを警告する必要があります。 3つのフィールドがすべて空の場合は、特定のアラートメッセージを生成する必要があります。何か書きましたが、常に「NaN」アラートが表示されます。 NaNの代わりに、私は "Introduceti cel putin un numar"を得たいと思っています。これで私を助けることができますか? ここに私のコード(htmlとjavascript)があります。JavaScriptが3つの数字アルゴリズムで空である場合のjavascriptの特定の警告

ありがとうございます!

<!doctype HTML> 
<html> 

<head> 
    <title>Algoritmi</title> 
    <script src="point2.js"> 

    </script> 
</head> 

<body> 
    <form name="mainForm" style="text-align:center"> 
     <input type="text" name="textBox1" /> <br/> 
     <input type="text" name="textBox2" /> <br/> 
     <input type="text" name="textBox3" /> <br/> 
     <input type="button" value="Afiseaza maxim" onclick=" javascript: calcMax()" /> 
    </form> 
</body> 

</html> 

とJavaScript:

function calcMax() { 
    var a = parseInt(document.mainForm.textBox1.value); 
    var b = parseInt(document.mainForm.textBox2.value); 
    var c = parseInt(document.mainForm.textBox3.value); 
    var max; 
    if (a == null || a == "" && b == null || b == "" && c == null || c == "") { 
     alert("Introduceti cel putin un numar"); 
    } 
    else { 
     if (a > b) { 
      if (a > c) { 
       max = a 
      } 
      else { 
       max = c; 
      } 
     } 
     else { 
      if (b > c) { 
       max = b 
      } 
      else { 
       max = c; 
      } 
     } 
     alert(max); 
    } 
} 

答えて

0

注1でのisNaN(a)の条件を追加しました:&&||よりも優先順位が高い、あなたは括弧内の||テストをラップする必要があります。

注2:lengthの値が空であるかどうかを確認するだけで確認できます。

注3: 3つの値の最大値を計算するには、Math.maxを使用します。

function calcMax() { 
    // no need for parseInt 
    var a = document.mainForm.textBox1.value;    // you can add .trim() at the end to remove surrounding spaces 
    var b = document.mainForm.textBox2.value;    // ... 
    var c = document.mainForm.textBox3.value;    // ... 
    var max; 

    if (a.length * b.length * c.length === 0) {    // at least one of the inputs value's length is 0 
     alert("You must fill the inputs"); 
    } 
    else if(isNaN(a) || isNaN(b) || isNaN(c)) {    // at least one of the inputs value is not a valid number 
     alert("inputs must be filled with numbers"); 
    } 
    else { 
     max = Math.max(a, b, c);       // a, b and c are all numbers, use Math.max to calculate the maximum (at this stage a, b and c are string, Math.max will internally convert them to numbers) 
     alert(max); 
    } 
} 
+0

ありがとう、イブラヒム、それは動作します! :)と説明もありがとう! –

+0

あなたは大歓迎です! –

1

あなたはそれがNaN(非数)となりますintに空の値を変換するので、あなたはisNaN(値)を使用して、この条件を処理する必要があります。 isNaNは、値が他の偽の数値でない場合にtrueを返します。

入力値が空の場合、aの値はparseInt()ステップの後にNaNになります。今私はあなたの場合は条件

function calcMax() { 
 
    var a = parseInt(document.mainForm.textBox1.value); 
 
    var b = parseInt(document.mainForm.textBox2.value); 
 
    var c = parseInt(document.mainForm.textBox3.value); 
 
    var max; 
 
    
 
    if ((a == null || a == "" || isNaN(a)) && (b == null || b == "" || isNaN(b)) && (c == null || c == "" || isNaN(c))) { 
 
     alert("Introduceti cel putin un numar"); 
 
    } 
 
    else { 
 
     if (a > b) { 
 
      if (a > c) { 
 
       max = a 
 
      } 
 
      else { 
 
       max = c; 
 
      } 
 
     } 
 
     else { 
 
      if (b > c) { 
 
       max = b 
 
      } 
 
      else { 
 
       max = c; 
 
      } 
 
     } 
 
     alert(max); 
 
    } 
 
}
<!doctype HTML> 
 
<html> 
 

 
<head> 
 
    <title>Algoritmi</title> 
 
    <script src="point2.js"> 
 

 
    </script> 
 
</head> 
 

 
<body> 
 
    <form name="mainForm" style="text-align:center"> 
 
     <input type="text" name="textBox1" /> <br/> 
 
     <input type="text" name="textBox2" /> <br/> 
 
     <input type="text" name="textBox3" /> <br/> 
 
     <input type="button" value="Afiseaza maxim" onclick=" javascript: calcMax()" /> 
 
    </form> 
 
</body> 
 

 
</html>

+0

ありがとう、Dinesh!できます! :D –

関連する問題