Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3

Thread: how to code

  1. #1
    New to the CF scene
    Join Date
    Jan 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs down how to code

    Please can someone help me! I am trying to make a code (menu) that inputs 2 numbers and at the user's option finds the sum, difference, product or quotient. Is there anyone who can help?

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,731
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Sounds like homework!

    As a general rule, the people helping out in this forum don't write code for others (especially code that appears to be for homework), but try to help with fixing code that doesn't work. You may perhaps get someone to write this script for you, but you'll be far more likely to get help if you have made a substantial effort and written some code yourself. Then someone here will almost certainly help you correct/improve your work.


    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    3,763
    Thanks
    23
    Thanked 548 Times in 547 Posts
    This is by no means elegant and probable not what you meant by menu (that's the problem with not giving some code to start with) And worst it probable wont be accepted, but I had some free time.
    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <title>Title of the document</title>
    <style type="text/css">
    textarea
    {
    	resize:none;
    	height:20px;
    	float:left;
    }
    button
    {
    	display: list-item;
    	list-style: none;
    }
    </style>
    </head>
    <body onload="document.getElementById('first').focus();">
    <div id="text">Enter a number. Then tab</div>
    <textarea id="first" cols="3" maxlength="5" tabindex=1></textarea>
    <div id="sign" style="height:20px;width:30px;text-align:middle;padding-left:23px;float:left;">?</div>
    <textarea id="second" cols="3" maxlength="5" tabindex=2></textarea>
    <div id="equal" style="height:20px;width:30px;text-align:center; float:left;">=</div>
    <textarea id="answer" cols="3" maxlength="5" tabindex=-1></textarea>
    <br /><br />
    <button onclick="action('Sum');" tabindex=3>Sum</button>
    <button onclick="action('Difference');" tabindex=4>difference</button>
    <button onclick="action('Product');" tabindex=5>product</button>
    <button onclick="action('Quotient');" tabindex=6>quotient</button>
    <script type="text/javascript">
    function action(control)
    {
    	if(control == 'Sum')
    	{
    		document.getElementById('sign').innerHTML = "+";
    		document.getElementById('answer').value=Number(document.getElementById('first').value) + Number(document.getElementById('second').value);
    	}
    	else if(control == 'Difference')
    	{
    		document.getElementById('sign').innerHTML = "-";
    		document.getElementById('answer').value=Number(document.getElementById('first').value) - Number(document.getElementById('second').value);
    	}
    	else if(control == 'Product')
    	{
    		document.getElementById('sign').innerHTML = "*";
    		document.getElementById('answer').value=Number(document.getElementById('first').value) * Number(document.getElementById('second').value);
    	}
    	else if(control == 'Quotient')
    	{
    		document.getElementById('sign').innerHTML = "/";
    		document.getElementById('answer').value=Number(document.getElementById('first').value) / Number(document.getElementById('second').value);
    	}
    }
    </script>
    </body>
    </html>


  •  

    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
    •