www.webdeveloper.com
Results 1 to 8 of 8

Thread: JS calculation

  1. #1
    Join Date
    Mar 2014
    Posts
    4

    JS calculation

    i am now developing a wed-based system involving JS calculation which is ADD operator in form.
    but i still can't figure this out

    function jumkerusi()
    {


    var a = parseInt(document.forms[0].jumbyrsofa.value);
    var b = parseInt(document.forms[0].jumbyrvip.value);
    var c = parseInt(document.forms[0].jumbyrplastik.value);
    var s = parseInt(document.forms[0].jumbyrkerusi.value);

    s= a+b+c;


    document.forms[0].jumbyrkerusi.value =s;
    }

  2. #2
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,432
    What does your HTML look like?
    What does the error console say?

    Try not to double post.

  3. #3
    Join Date
    Mar 2014
    Posts
    4
    var a, b and c containing 3 integers which to be sum up using ADD operator and the answer would be in var s.. for example when 1+2+3 turns out to be 123 instead equal to 6. sorry i know it's kinda basic but im still learning this.. tq for ur reply

  4. #4
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,432

    Exclamation

    Quote Originally Posted by JMRKER View Post
    What does your HTML look like?
    What does the error console say?

    Try not to double post.
    Quote Originally Posted by ieyza View Post
    var a, b and c containing 3 integers which to be sum up using ADD operator and the answer would be in var s.. for example when 1+2+3 turns out to be 123 instead equal to 6. sorry i know it's kinda basic but im still learning this.. tq for ur reply
    Since you did not answer the 1st post questions ,
    the following is a simulation of your problem without the HTML elements.

    Code:
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8" />
    
    <title> Untitled </title>
    
    </head>
    <body>
    <script type="text/javascript">
    function jumkerusi() {
      var sa = '1',  sb = '2', sc = '3', ss = ''; 
      var a = parseInt(sa);
      var b = parseInt(sb);
      var c = parseInt(sc);
    //  var s = parseInt(ss);
      var s= a+b+c;
      alert('With parseInt: '+s);
    } 
    function jumkerusiWithoutParseInt() {
      var sa = '1',  sb = '2', sc = '3', ss = ''; 
      var a = sa;
      var b = sb;
      var c = sc;
      var s= a+b+c;
      alert('Without parseInt: '+s);
    } 
    
    window.onload = function() {
      jumkerusiWithoutParseInt();
      jumkerusi();
    }
    </script>
    
    </body>
    </html>

  5. #5
    Join Date
    Oct 2013
    Posts
    592
    Or possibly??

    Code:
    function jumkerusi(){
    	var a = parseInt(document.forms[0].jumbyrsofa.value);
    	var b = parseInt(document.forms[0].jumbyrvip.value);
    	var c = parseInt(document.forms[0].jumbyrplastik.value);
    	var s = parseInt(document.forms[0].jumbyrkerusi.value);
       s= 1*a+1*b+1*c;	// The "1*" forces the math rather than concatenating values
       document.forms[0].jumbyrkerusi.value =s;
    }

  6. #6
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,432
    That's fine, multiplying by 1, but that is the purpose of what the parseInt function does, so it is not needed.

    The problem in the original post *may* be related to the HTML entry value.
    If there are spaces it might not be converting correctly. That information has never been supplied.

  7. #7
    Join Date
    Mar 2014
    Posts
    4
    thnx Kevin2.. it really works.. thnk you very much for ur help

  8. #8
    Join Date
    Oct 2012
    Location
    Croatia
    Posts
    255
    Quote Originally Posted by ieyza View Post
    thnx Kevin2.. it really works.. thnk you very much for ur help
    It works now but the big question is will it work some other time? For example:

    If there are spaces it might not be converting correctly. That information has never been supplied.
    You're not paying attention to what other forum members asked you about! So this may be just temporary solution until the next time when you come back with the same kind of problem.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles