Advertisement
Guest User

Untitled

a guest
Jan 21st, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.42 KB | None | 0 0
  1. <html>
  2. <head>
  3. <title>
  4. :: Latihan Javascript ::
  5. </title>
  6. </head>
  7. <body>
  8. <h1>Simple Calculator</h1>
  9. <form>
  10. <table border="1" cellpadding="4px" cellspacing="6px">
  11. <tr>
  12. <td colspan="4" align="center"> <input type="text" id="angka" value="" > </td>
  13. </tr
  14. <tr>
  15. <td align="center"><input type="button" value="1" onclick="pencet(this)"/></td>
  16. <td align="center"><input type="button" value="2" onclick="pencet(this)"/></td>
  17. <td align="center"><input type="button" value="3" onclick="pencet(this)"/></td>
  18. <td align="center"><input type="button" value="+" onclick="pencet(this)"/></td>
  19. </tr>
  20. <tr>
  21. <td align="center"><input type="button" value="4" onclick="pencet(this)"/></td>
  22. <td align="center"><input type="button" value="5" onclick="pencet(this)"/></td>
  23. <td align="center"><input type="button" value="6" onclick="pencet(this)"/></td>
  24. <td align="center"><input type="button" value="-" onclick="pencet(this)"/></td>
  25. </tr>
  26. <tr>
  27. <td align="center"><input type="button" value="7" onclick="pencet(this)"/></td>
  28. <td align="center"><input type="button" value="8" onclick="pencet(this)"/></td>
  29. <td align="center"><input type="button" value="9" onclick="pencet(this)"/></td>
  30. <td align="center"><input type="button" value="*" onclick="pencet(this)"/></td>
  31. </tr>
  32. <tr>
  33. <td align="center"><input type="button" value="0" onclick="pencet(this)"/></td>
  34. <td align="center"><input type="reset" value="C" id="clear" /></td>
  35. <td align="center"><input type="button" value="." onclick="pencet(this)"/></td>
  36. <td align="center"><input type="button" value="/" onclick="pencet(this)"/></td>
  37. </tr>
  38. <tr>
  39. <td colspan=4>
  40. <input type=button value="=" onclick="result()" >
  41. </td>
  42. </tr>
  43. </table>
  44. </form>
  45. <style type="text/css">
  46. #angka{
  47. font-size: 30pt;
  48. text-align: right;
  49. width: 220px;
  50. }
  51. input[type="button"], input[type="reset"]{
  52. width: 100%;
  53. }
  54. </style>
  55. <script>
  56. var angka = '';
  57. var hasil = 0;
  58. function pencet(e){
  59. if (hasil > 0 || document.getElementById('angka').value == 0){
  60. document.getElementById('angka').value = '';
  61. hasil = 0;
  62. }
  63. angka = e.value;
  64. document.getElementById('angka').value += e.value ;
  65. angka += angka;
  66. }
  67.  
  68. function result(){
  69. angka2 = document.getElementById('angka').value;
  70. hasil = eval(angka2);
  71. if (hasil !== undefined) {
  72. document.getElementById('angka').value = hasil;
  73. }
  74. }
  75. </script>
  76. </body>
  77. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement