-3

I am looking for correct code so function named "finder()" can execute and find the highest value in Array named "sum" . When press "Find max" button result is :"[object HTMLInputElement] " Thank you for any help! Here is code:

<html>
  <head>
   <title>Max</title>
    <meta charset="utf-8">
    <script type="text/javascript">
      var sum = document.getElementsByName('addends');
      function add() {
          var x = document.getElementById('apendiks');
          x.innerHTML+="<br/><input type='text' name='addends' size='7'>";
        }

       function finder() {  
          var max = sum[0];           
           for(i=0; i<sum.length;i++){

                if(sum[i] > max){
                   max = sum[i];
                }                 
           } 

          document.getElementById('showMaxValue').innerHTML = max;
        }

        function gather() {

          var total=0;
          for(i=0; i < sum.length; i++){

              total+=parseFloat(sum[i].value);
          }
          document.getElementById('score').innerHTML = total;
        }
        </script>
  </head>
  <body>
   <p><input type="button" onclick="add()" value="Add a new box"> </p>
   <p><input type="button" onclick="" value="-"> </p>

  <div id="apendiks"></div>
   <p><input type="button" onclick="gather()" value="Total sum"> </p>
    <p>Score: <b id="score"></b></p>
   <p><input type="button" onclick="finder()" value="Find max"> </p>
   <p id="showMaxValue"></p>
  </body>
 </html> 
The Process
  • 5,913
  • 3
  • 30
  • 41
Car Lazar
  • 13
  • 4
  • what if I entered `398 49 fdr` into the text field `'addends'` ? – RomanPerekhrest Apr 16 '16 at 13:16
  • Sum[] is an array of html input objects. I think you are trying to get the value out of the input, but instead you are comparing input elements to each other instead of the value they contain. – klog Apr 16 '16 at 13:17
  • Multiple problems ... please see http://stackoverflow.com/questions/14028959/why-does-jquery-or-a-dom-method-such-as-getelementbyid-not-find-the-element – charlietfl Apr 16 '16 at 13:19
  • @charlietfl but he got the result `When press "Find max" button result is :"[object HTMLInputElement] "` – gurvinder372 Apr 16 '16 at 13:20

2 Answers2

0

var sum = document.getElementsByName('addends');

This line will give you a list of Nodes, not an Array.

In your finder method, you need to treat it as such

  function finder() 
  {
       var max = paeseInt(sum[0].value) ;
       for(i=0; i<sum.length;i++){
            if(parseInt(sum[i].value) > max && !isNaN(sum[i].value)){ //assuming that strings should be ignored
               max = parseInt(sum[i].value) ;
            }
       } 
       document.getElementById('showMaxValue').innerHTML = max;
  }
gurvinder372
  • 66,980
  • 10
  • 72
  • 94
0

Use .value to get the value of input field

<html>
  <head>
   <title>Max</title>
    <meta charset="utf-8">
    <script type="text/javascript">
      var sum = document.getElementsByName('addends');
        function add() {

          var x = document.getElementById('apendiks');

          x.innerHTML+="<br/><input type='text' name='addends' size='7'>";

        }

        function finder() {

          var max = sum[0].value;

           for(i=0; i<sum.length;i++){



                if(sum[i].value > max){


                   max = sum[i].value;

                }

           } 

          document.getElementById('showMaxValue').innerHTML = max;
        }





        function gather() {



          var total=0;
          for(i=0; i < sum.length; i++){

              total+=parseFloat(sum[i].value);
          }
          document.getElementById('score').innerHTML = total;
        }

        </script>
  </head>

  <body>
   <p><input type="button" onclick="add()" value="Add a new box"> </p>
   <p><input type="button" onclick="" value="-"> </p>


  <div id="apendiks"></div>
   <p><input type="button" onclick="gather()" value="Total sum"> </p>
    <p>Score: <b id="score"></b></p>
   <p><input type="button" onclick="finder()" value="Find max"> </p>
   <p id="showMaxValue"></p>
  </body>
 </html> 
Munawir
  • 3,346
  • 9
  • 33
  • 51