I wrote the converter of values ​​but for some reason it does not calculate and displays "0" everywhere, tell me why? If it doesn't make it difficult for you write a piece js to output the result (

<html> <head> <meta charset="utf-8"> <head/> <body> <form> <label>Enter a value</label> <select name="from" id="from"> <option value="0" selected="selected">Killometres</option> <option value="1">Milles</option> <option value="2">Futs</option> <option value="2">Yards</option> <option value="2">Dums</option> </select> <br/> <br/> <label>Enter a count </label> <input name="cash" id="cash" class="is" type="text"/> <br/> <br/> <label>Enter a change value </label> <select name="to" id="to"> <option value="0" >Killometres</option> <option value="1"selected="selected">Milles</option> <option value="2">Futs</option> <option value="3">Yards</option> <option value="4">Dums</option> </select> <br/> <br/> <input type=button value="Convert" onclick='GiveResult();'> <br/> <br/> <label><span id="result" class="result"></span></label> </form> <script type="text/javascript"> c = new Array(); n = new Array(); c[0] = 1; n[0] = "Killometres"; c[1] = 0,62; n[1] = "Milles"; c[2] = 4; n[2] = "Futs"; c[3] = 5; n[3] = "Yards"; c[4] = 6; n[4] = "Dums"; function GiveResult() { var res, vfrom, vto, vcash; vcash = document.getElementById("cash").value; vfrom = document.getElementById("from").value; vcash = vcash.replace(',', '\.'); vcash = vcash.replace(' ', ''); vcash = vcash.replace(' ', ''); vto = document.getElementById("to").value; res = c[vto] * vcash / c[vfrom], 2; res = res.toFixed(0); res = res.toString(); res = res.replace('\.', ','); res = "<span class='result'> Результат перевода: " + res + "</span>&nbsp;" + n[vto]; document.getElementById("result").innerHTML = res; } </script> </body> </html> 

  • one
    what the following lines mean: c[1] = 0,62; and c[vfrom], 2 ? - Grundy
  • 1 km = 0.62 miles, vfrom = document.getElementById ("from"). Value; and from the identifiers of the value that I chose, but for some reason it outputs 0 - The Marafonskiy
  • even when removed vfrom does not even multiply res = c [vto] * vcash; - The Marafonskiy

3 answers 3

As Grundy showed above, the first thing that hinders is c[1] = 0,62 => c[1] = 0.62 and two in calculations ... res = c[vto] * vcash / c[vfrom], 2;

 c = new Array(); n = new Array(); c[0] = 1; n[0] = "Killometres"; c[1] = 0.62 n[1] = "Milles"; c[2] = 4; n[2] = "Futs"; c[3] = 5; n[3] = "Yards"; c[4] = 6; n[4] = "Dums"; function GiveResult() { var res, vfrom, vto, vcash; vcash = document.getElementById("cash").value; vfrom = document.getElementById("from").value; vto = document.getElementById("to").value; res = c[vto] * vcash / c[vfrom]; res = res.toFixed(2); res = res.toString(); res = "<span class='result'> Результат перевода: " + res + "</span>&nbsp;" + n[vto]; document.getElementById("result").innerHTML = res; } 
 <form> <label>Enter a value</label> <select name="from" id="from"> <option value="0" selected="selected">Killometres</option> <option value="1">Milles</option> <option value="2">Futs</option> <option value="2">Yards</option> <option value="2">Dums</option> </select> <br/> <br/> <label>Enter a count</label> <input name="cash" id="cash" class="is" type="text" /> <br/> <br/> <label>Enter a change value</label> <select name="to" id="to"> <option value="0">Killometres</option> <option value="1" selected="selected">Milles</option> <option value="2">Futs</option> <option value="3">Yards</option> <option value="4">Dums</option> </select> <br/> <br/> <input type=button value="Convert" onclick='GiveResult();'> <br/> <br/> <label><span id="result" class="result"></span> </label> </form> 

  • The last two, by the way, does not interfere. She just does nothing - Grundy
  • @Grundy agree .. - C.Raf.T

The problem is in the operator , which is used in the string

 c[1] = 0,62; 

As a result of the execution of this line, the cell value will not be the expected 0.62 , but 0 . Therefore, when using this value in the formula

 c[vto] * vcash / c[vfrom] 

The result will be 0 or Infinity

In addition, the purpose of using the operator is not clear ,

 res = c[vto] * vcash / c[vfrom], 2; 

The following note on working with the entered data:

There is no need to write replace on a separate line, since this function returns a string, the following can be applied directly to the result. In addition, the logic is not completely clear: both the whitespace character is removed and the character , changed to .

You can use the following approach instead.

 .replace(/\s+/g,'').replace(',','.'); 

After the change, it is better to bring the string to a number, for example using parseFloat

Next: work with the result.

toFixed already returns a string. No need to call toString again. When replacing a point with a comma, there is no need to escape the point, because the string is used, not a regular expression.

Further:

Arrays can be declared immediately filled with the array literal [...]

for example: c = [1,0.62,4,5,6]

And .toFixed(0) : .toFixed(0) , rounds the number down to a whole, and if the result was less than 0.5 , the answer will be 0 .

As a result, it may turn out like this:

 c = [1, 0.62, 4, 5, 6]; n = ["Killometres", "Milles", "Futs", "Yards", "Dums"]; function GiveResult() { var res, vfrom, vto, vcash; vfrom = document.getElementById("from").value; vcash = parseFloat(document.getElementById("cash").value.replace(',', '.').replace(/\s+/g, '')); vto = document.getElementById("to").value; res = (c[vto] * vcash / c[vfrom]).toFixed(0).replace('.', ','); document.getElementById("result").innerHTML = "<span class='result'> Результат перевода: " + res + "</span>&nbsp;" + n[vto]; } 
 <form> <label>Enter a value</label> <select name="from" id="from"> <option value="0" selected="selected">Killometres</option> <option value="1">Milles</option> <option value="2">Futs</option> <option value="2">Yards</option> <option value="2">Dums</option> </select> <br/> <br/> <label>Enter a count</label> <input name="cash" id="cash" class="is" type="text" /> <br/> <br/> <label>Enter a change value</label> <select name="to" id="to"> <option value="0">Killometres</option> <option value="1" selected="selected">Milles</option> <option value="2">Futs</option> <option value="3">Yards</option> <option value="4">Dums</option> </select> <br/> <br/> <input type=button value="Convert" onclick='GiveResult();'> <br/> <br/> <label><span id="result" class="result"></span> </label> </form> 

    You have с[vform] = undefined, and in the line

     res = c[vto] * vcash / c[vfrom], 2; 

    There is a division into it. Accordingly, at 0. In the end, and it turns out 0.

    • one
      res[vform] - Grundy
    • @Grundy already noticed, fixed, thanks. - Vitaliy RS
    • one
      well, and then it is not NULL - there is no such thing in javascript, and undefined - Grundy
    • @Grundy importantly, it would be clear that the array element has no value. Thanks again for correcting) - Vitaliy RS