SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Member
    Join Date
    Jul 2008
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Red face Help with writing small math app

    Hi all, I need some guidance on the writing of a small calculator. I have looked at other calculators and played with working from there but its not quite what I want.

    Here's what I have to calculate:

    20 hours = 1000 x cost - expense (that is determined by the users first choice...more clearly....

    The form would be like this:
    how many hours do you want to work:
    a. 20 (1000) behind the scenes: 1000 per 20hrs
    b. 40 (2000)
    c. 60 (3000)

    what is the cost of living where you work:
    a. high = 8
    b. medium = 6
    c. low = 4

    so then I would get the value of hours x cost of living
    and then I have to subtract transportaion which if
    20hrs = x
    40hrs = y
    60hrs = z (I have the values for these already)

    so the user would be choosing the first two values from a check box say....

    so then I have to take the value from those functions (lets call it I)
    and then do :
    I x 4 = monthly
    I x 12 = Yearly
    I /160 = hourly

    I understand what I need to do and even some of the operators its just putting it all together to work and getting the syntax and such ...(guess thats the whole of it though huh?)

    Thanks in advance to Code masters out there who volunteer help...big appreciations...
    T.

  2. #2
    We're from teh basements.
    Join Date
    Apr 2007
    Posts
    1,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by CodeGrrr View Post
    so then I would get the value of hours x cost of living
    and then I have to subtract transportaion which if
    20hrs = x
    40hrs = y
    60hrs = z (I have the values for these already)
    If x, y, and z are all pre-determined values, you can either use a three-case switch statement with the number of hours as cases, or use the ternary operator thusly:

    Code:
    // if hours is neither 20 or 40, then it must be 60
    var Ctrans = (hours == 20) ? x : ((hours == 40) ? y : z);

  3. #3
    SitePoint Member
    Join Date
    Jul 2008
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hmm , I'll have to look up what you mean, thanks for replying though! Here is what I started but now I think maybe I am off base?
    Code:
    <script language="javascript">
    
    
    
    function doCalc () {
    var sqFeet = document.getElementById('sqFeet').value;
    var hrWork = document.getElementById('hrWork').value;
    
    var grossMoney = document.getElementById('grossMoney').setAttribute('value', sqFeet*hrWork);
    
    document.getElementById('grossMoney').onchange = doCalc;
    
    
    </script>
    Please select how many hours you will work weekly:
    <select id="sqFeet">
        <option value="">Select: </option>
        <option value="1000">20</option>
        <option value="2000">40</option>
        <option value="3000">60</option>
       
    </select>
    <input type="text" id= "grossMoney" />
    I thought I would be able to define everything and then just....I'm not sure now....

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Something like this should work.

    Code html4strict:
    <form id="myForm">
    <fieldset>
    	<legend>How many hours do you want to work:</legend>
    	<select name="hours">
    		<option value="1000">20</option>
    		<option value="2000">40</option>
    		<option value="3000">60</option>
    	</select>
    </fieldset>
    <fieldset>
    	<legend>What is the cost of living where you work?</legend>
    	<select name="costOfLiving">
    		<option value="8">High</option>
    		<option value="6">Medium</option>
    		<option value="4">Low</option>
    	</select>
    </fieldset>
    <p><input type="submit"></p>
    <p>Result of hours * cost of living - transportation:</p>
    <p><input type="text" name="result"></p>
    <p>
    	<label>Monthly: <input name="monthly"></label>
    	<label>Yearly: <input name="yearly"></label>
    	<label>Hourly: <input name="hourly"></label>
    </p>
    </form>

    Code javascript:
    document.getElementById('myForm').onsubmit = function () {
    	var hours = this.elements.hours.value;
    	var costOfLiving = this.elements.costOfLiving.value;
    	var transportation = [];
    	var result;
     
    	transportation[1000] = 400;
    	transportation[2000] = 800;
    	transportation[3000] = 1200;
    	result = hours * costOfLiving - transportation[hours];
     
    	this.elements.result.value = result;
    	this.elements.monthly.value = result * 4;
    	this.elements.yearly.value = result * 52;
    	this.elements.hourly.value = result / 160;
     
    	return false;
    }
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  5. #5
    SitePoint Member
    Join Date
    Jul 2008
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hmm...it looks beautiful but it doesn't work, maybe I have something wrong in my page? because nothing happens when I submit, I'm trying to find the difference of using
    Code:
    var hours = this.elements.hours.value;
    or
    Code:
    var hours = getElementById('hours').value;
    how to know when to use what?

    Thanks for responding! Def. gives me something to work on and with!
    T.

  6. #6
    SitePoint Addict
    Join Date
    Dec 2007
    Posts
    207
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Make sure that
    a) the code is after the form's markup.
    OR
    b) you put this function in the window's onload.


    I cleaned up the JavaScript a bit. I don't think its a good idea to create an array with 3000 items.

    Code javascript:
    document.getElementById('myForm').onsubmit = function(){
    	var els = this.elements
    	 ,hours = els.hours.value
    	 ,costOfLiving = els.costOfLiving.value
    	 ,result;
     
    	result = hours * costOfLiving - ((parseInt(hours) / 50) * 20);
     
    	els.result.value = result;
    	els.monthly.value = result * 4;
    	els.yearly.value = result * 52;
    	els.hourly.value = result / 160;
     
    	return false;
    }
    mmj

  7. #7
    SitePoint Member
    Join Date
    Jul 2008
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for responding, I did finally get it to work, here's my code if anyone could use it
    Code:
    <script type="text/javascript">
    window.onload = function () {
    
    document.getElementById('profitCalc').onsubmit = function  doCalc () {
    	var form = document.getElementById("profitCalc");
        var hours = document.getElementById('hours').value;
        var costOfLiving = document.getElementById('costOfLiving').value;
    	var materials = document.getElementById('materials').value;
        var transportation = [];
        var result;
       
        transportation[1000] = 70;
        transportation[2000] = 139;
        transportation[3000] = 209;
        result = hours * costOfLiving - transportation[hours] - materials * hours;
       
        this.elements.result.value = result;
        this.elements.monthly.value = result * 4;
        this.elements.yearly.value = result * 52;
        this.elements.hourly.value = result / 160;
       
        return false;
    }
    
    
    }
    
    
    </script>
    Thanks for the help everyone!


Bookmarks

Posting Permissions

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