javascript - Calculator Coding - adding fraction buttons -
javascript - Calculator Coding - adding fraction buttons -
i got sample calculator javascript
kit, , i'm trying modify add together fraction buttons (convert decimal fraction & vice versa, using numbers create fraction, illustration pressing 2 button pressing 4 , makes 2/4 or 1/2, , doing same number after partition symbol it's 2/4 divided 1/2. here's calculator code, head script , body itself.
<script language="javascript" id="calculator"> function addchar(input, character) { if(input.value == null || input.value == "0") input.value = character else input.value += character } function cos(form) { form.display.value = math.cos(form.display.value); } function sin(form) { form.display.value = math.sin(form.display.value); } function tan(form) { form.display.value = math.tan(form.display.value); } function sqrt(form) { form.display.value = math.sqrt(form.display.value); } function ln(form) { form.display.value = math.log(form.display.value); } function exp(form) { form.display.value = math.exp(form.display.value); } function deletechar(input) { input.value = input.value.substring(0, input.value.length - 1) } function changesign(input) { if(input.value.substring(0, 1) == "-") input.value = input.value.substring(1, input.value.length) else input.value = "-" + input.value } function compute(form) { form.display.value = eval(form.display.value) } function square(form) { form.display.value = eval(form.display.value) * eval(form.display.value) } function checknum(str) { (var = 0; < str.length; i++) { var ch = str.substring(i, i+1) if (ch < "0" || ch > "9") { if (ch != "/" && ch != "*" && ch != "+" && ch != "-" && ch != "." && ch != "(" && ch!= ")") { alert("invalid entry!") homecoming false } } } homecoming true } </script>
body <center><div id="calculator"> <form name="sci-calc"> <table cellspacing="0" cellpadding="1"> <tr> <td colspan="5" align="center"><input name="display" value="0" size="28" maxlength="25"></td> </tr> <tr> <td align="center"><input type="button" value=" exp " onclick="if (checknum(this.form.display.value)) { exp(this.form) }"></td> <td align="center"><input type="button" value=" 7 " onclick="addchar(this.form.display, '7')"></td> <td align="center"><input type="button" value=" 8 " onclick="addchar(this.form.display, '8')"></td> <td align="center"><input type="button" value=" 9 " onclick="addchar(this.form.display, '9')"></td> <td align="center"><input type="button" value=" / " onclick="addchar(this.form.display, '/')"></td> </tr> <tr> <td align="center"><input type="button" value=" ln " onclick="if (checknum(this.form.display.value)) { ln(this.form) }"></td> <td align="center"><input type="button" value=" 4 " onclick="addchar(this.form.display, '4')"></td> <td align="center"><input type="button" value=" 5 " onclick="addchar(this.form.display, '5')"></td> <td align="center"><input type="button" value=" 6 " onclick="addchar(this.form.display, '6')"></td> <td align="center"><input type="button" value=" * " onclick="addchar(this.form.display, '*')"></td> </tr> <tr> <td align="center"><input type="button" value=" sqrt " onclick="if (checknum(this.form.display.value)) { sqrt(this.form) }"></td> <td align="center"><input type="button" value=" 1 " onclick="addchar(this.form.display, '1')"></td> <td align="center"><input type="button" value=" 2 " onclick="addchar(this.form.display, '2')"></td> <td align="center"><input type="button" value=" 3 " onclick="addchar(this.form.display, '3')"></td> <td align="center"><input type="button" value=" - " onclick="addchar(this.form.display, '-')"></td> </tr> <tr> <td align="center"><input type="button" value=" sq " onclick="if (checknum(this.form.display.value)) { square(this.form) }"></td> <td align="center"><input type="button" value=" 0 " onclick="addchar(this.form.display, '0')"></td> <td align="center"><input type="button" value=" . " onclick="addchar(this.form.display, '.')"></td> <td align="center"><input type="button" value=" +/- " onclick="changesign(this.form.display)"></td> <td align="center"><input type="button" value=" + " onclick="addchar(this.form.display, '+')"></td> </tr> <tr> <td align="center"><input type="button" value=" ( " onclick="addchar(this.form.display, '(')"></td> <td align="center"><input type="button" value="cos" onclick="if (checknum(this.form.display.value)) { cos(this.form) }"></td> <td align="center"><input type="button" value=" sin" onclick="if (checknum(this.form.display.value)) { sin(this.form) }"></td> <td align="center"><input type="button" value=" tan" onclick="if (checknum(this.form.display.value)) { tan(this.form) }"></td> <td align="center"><input type="button" value=" ) " onclick="addchar(this.form.display, ')')"></td> </tr> <tr> <td align="center"><input type="button" value="clear" onclick="this.form.display.value = 0 "></td> <td align="center" colspan="3"><input type="button" value="backspace" onclick="deletechar(this.form.display)"></td> <td align="center"><input type="button" value="enter" name="enter" onclick="if (checknum(this.form.display.value)) { compute(this.form) }"></td> </tr> </table> </form> </div>
javascript jquery
Comments
Post a Comment