Results 1 to 4 of 4

Thread: Javascript Calculation

  1. #1
    Join Date
    Aug 2010

    Question Javascript Calculation

    Hi there, i'm trying to create a form that calculates numbers although 2 of the fields require a number that could be between 0.3 and 1

    the calculation itself works until you use a number less than 1 and the output is just zero

    here is my code

    <title> Calc</title>
    <script language="javascript">
    function addNumbers()
    var val1 = parseInt(document.getElementById("value1").value);
    var val2 = parseInt(document.getElementById("value2").value);
    var val3 = parseInt(document.getElementById("value3").value);
    var val4 = parseInt(document.getElementById("value4").value);
    var val5 = parseInt(document.getElementById("value5").value);
    var ansD = document.getElementById("answer");
    var ansE = document.getElementById("value6");
    ansD.value = val1 * val2;
    ansE.value = val2 * val3 * val4 * val5;


    <p>Average Cost per kW Installed (&pound
    <input type="text" id="value1" name="value1" value="4000"/>
    <p>Size of the Array (kWp)
    <input type="text" id="value2" name="value2" value="4"/>
    <input type="button" name="Sumbit" value="Click here" onClick="javascript:addNumbers()"/>
    <p>Estimated Installed Cost (&pound
    <input type="text" id="answer" name="answer" value=""/>
    <p>Load factor
    <input type="text" id="value3" name="value3" value="0.8"/>
    <p>Solar Radiation Factor
    <input type="text" id="value4" name="value4" value="1073"/>
    <p>Overshading Factor
    <input type="text" id="value5" name="value5" value="1"/>
    <p>Estimated Output (kWh/annum)
    <input type="text" id="value6" name="value6" value=""/>

  2. #2
    Join Date
    Mar 2010
    You just need to do some Basic Debugging 101 (post #1).

    Why are you using parseInt() if you are not using integers in your calculations?

    Use the correct parse function for the input data and your problem will be fixed.
    Last edited by tirna; 08-13-2010 at 07:55 AM.

  3. #3
    Join Date
    Aug 2010
    sorry i'm fairly new to javascript, i've been reading alot a in the forum and they are a great help but thanks for pointing me in the right direction

  4. #4
    Join Date
    Mar 2010
    you're welcome

Thread Information

Users Browsing this Thread

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

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