CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   JavaScript programming (http://www.codingforums.com/forumdisplay.php?f=2)
-   -   how to code (http://www.codingforums.com/showthread.php?t=286728)

kittyluv 01-30-2013 12:20 PM

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?

Philip M 01-30-2013 12:33 PM

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.

sunfighter 01-30-2013 05:57 PM

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>



All times are GMT +1. The time now is 01:30 PM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.