Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <html>
- <head>
- <script type="text/JavaScript">
- function get(eid) {return document.getElementById(eid);};
- function calculate(op){
- var a = parseFloat(get('a').value);
- var b = parseFloat(get('b').value);
- if (isNaN(a) || isNaN(b)) {
- alert('both inputs must be numbers');
- return;
- }
- var answer;
- switch (op) {
- case 'add': {
- answer = a + ' + ' + b + ' = ' + (a + b);
- break;
- }
- case 'subtract': {
- answer = a + ' - ' + b + ' = ' + (a - b);
- break;
- }
- case 'multiply': {
- answer = a + ' * ' + b + ' = ' + (a * b);
- break;
- }
- case 'divide': {
- if (0 == b) {
- alert('illegal value - cannot divide by 0');
- return;
- }
- answer = a + ' / ' + b + ' = ' + (a / b);
- break;
- }
- }
- get('resultAsTextBox').value = answer;
- }
- </script>
- </head>
- <body>
- <form>
- <p>
- Instructions:<br />
- Type two numbers and click a button.<br />
- The answer will then appear below.<br /><br />
- Input values:<br/ >
- First number to include: = <input id="a" type="text" /><br />
- Second number to include: = <input id="b" type="text" /><br /><br />
- <input type="button" value="add" onclick="calculate(this.value)" />
- <input type="button" value="subtract" onclick="calculate(this.value )" />
- <input type="button" value="multiply" onclick="calculate(this.value)" />
- <input type="button" value="divide" onclick="calculate(this.value)" /><br />
- Output value:<br />
- Result= <input type="text" id="resultAsTextBox" size="50" disabled="disabled" />
- <p>
- </form>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement