Berikut Code-nya:
001 | <html> |
002 | <head> |
003 | <style type="text/css"> |
004 | table, td, th |
005 | { |
006 | border:1px solid white; |
007 | background-color:black; |
008 | color:white; |
009 | } |
010 | </style> |
011 | <script type="text/javascript"> |
012 | function tambah() |
013 | { |
014 | var x = document.test4.input.value; |
015 | var y = document.test4.input2.value; |
016 | document.test4.hasil.value = Number(x) + Number(y); |
017 | if(x =="" || y ==""){ |
018 | alert("Tidak boleh Kosong"); |
019 | } |
020 | else if (isNaN(document.test4.hasil.value)) |
021 | alert("Inputan hanya berupa angka"); |
022 | } |
023 | function kurang() |
024 | { |
025 | var x = document.test4.input.value; |
026 | var y = document.test4.input2.value; |
027 | document.test4.hasil.value = Number(x) - Number(y); |
028 | if(x =="" || y ==""){ |
029 | alert("Tidak boleh Kosong"); |
030 | } |
031 | else if (isNaN(document.test4.hasil.value)) |
032 | alert("Inputan hanya berupa angka"); |
033 | } |
034 | function bagi() |
035 | { |
036 | var x = document.test4.input.value; |
037 | var y = document.test4.input2.value; |
038 | document.test4.hasil.value = Number(x)/Number(y); |
039 | if(x =="" || y ==""){ |
040 | alert("Tidak boleh Kosong"); |
041 | } |
042 | else if (isNaN(document.test4.hasil.value)) |
043 | alert("Inputan hanya berupa angka"); |
044 | } |
045 | function kali() |
046 | { |
047 | var x = document.test4.input.value; |
048 | var y = document.test4.input2.value; |
049 | document.test4.hasil.value = Number(x) * Number(y); |
050 | if(x =="" || y ==""){ |
051 | alert("Tidak boleh Kosong"); |
052 | } |
053 | else if (isNaN(document.test4.hasil.value)) |
054 | alert("Inputan hanya berupa angka"); |
055 | } |
056 | function toHex() |
057 | { |
058 | var z=parseInt(document.test4.hasil.value); |
059 | document.test4.hasil2.value = z.toString(16); |
060 | } |
061 | function toOct() |
062 | { |
063 | var z=parseInt(document.test4.hasil.value); |
064 | document.test4.hasil2.value = z.toString(8); |
065 | } |
066 | function toBin() |
067 | { |
068 | var z=parseInt(document.test4.hasil.value); |
069 | document.test4.hasil2.value = z.toString(2); |
070 | } |
071 | </script> |
072 | </head> |
073 | <body> |
074 | <table align="center"> |
075 | <th colspan="2">KALKULATOR</th> |
076 | <form name="test4"> |
077 | <tr> |
078 | <td><input type="text" name="input"></td> |
079 | <td><input type="text" name="input2"></td> |
080 | </tr> |
081 | <tr> |
082 | <td><input type="button" onclick="tambah()" value="+"></td> |
083 | <td><input type="button" onclick="kurang()" value="-"></td> |
084 | </tr> |
085 | <tr> |
086 | <td><input type="button" onclick="kali()" value="x"></td> |
087 | <td><input type="button" onclick="bagi()" value=":"></td> |
088 | </tr> |
089 | <tr> |
090 | <td colspan="2" align="center"> |
091 | <input type="Reset" value="C"> |
092 | </td> |
093 | </tr> |
094 | <tr> |
095 | <td>Resuilt</td> |
096 | <td><input type="text" name="hasil"></td> |
097 | </tr> |
098 | <tr> |
099 | <td colspan="2" align="center"> |
100 | <input type="button" onclick="toBin()" value="toBin"> |
101 | <input type="button" onclick="toHex()" value="tohex"> |
102 | <input type="button" onclick="toOct()" value="toOct"></td> |
103 | </tr> |
104 | <tr> |
105 | <td colspan="2" align="center"> |
106 | <input type="text" name="hasil2"> |
107 | /td> |
108 | </tr> |
109 | </form> |
110 | </table> |
111 | </body> |
112 | </html> |
Kondisi Alert:
1. Jika inputan bukan angka dan;
2. Jika inputan kosong.
Konfersi : Biner(2), Octal(8), Hexa(16)

Tidak ada komentar:
Posting Komentar