Thursday, 15 July 2010

JAVASCRIPT: Getting values inside an array -


I'm having trouble calling the values ​​entered in numbers (I do not know what I should call it ... if there is a text box, then there should be a number box. Lol). If I enter "123456", the value of the sum should be "21", but what happens is that the value of the amount is "0123456".

  & lt; Input type = "number" name = "user" id = "input" maximum length = "6" size = "6" required & gt; & Lt; Input type = "button" onClick = "count ()" value = "calculation" & gt; & Lt; Script type = "text / javascript" & gt; Function Count () {var user = []; . User = document.getElementById ("input") value; If (user.length == 6) {var sum = 0; (I = 0; i & lt; user.length; i ++) for {sum = sum + user [i]; } Var ave = sum / 6; Window.alert ("Yoga:" + Yoga); Window.alert ("Average:" + Away); } And window.lert ("Please input 6 numbers."); } & Lt; / Script & gt;  

You break a string into parts and adding it back together . You need to know how to convert string to integer first, here's a very good answer:

  sum = sum + parseInt (user [i ], 10);  

should work


No comments:

Post a Comment