Foros del Web » Programando para Internet » Javascript »

Potencias en JavaScript

Estas en el tema de Potencias en JavaScript en el foro de Javascript en Foros del Web. Parecerá una duda un poco tonta ¿pero por qué si pongo "document.write(2^2)" me sale "0" y no "4"? Sin embargo con "document.write(Math.pow(2,2))" si obtengo el ...
  #1 (permalink)  
Antiguo 02/09/2010, 17:59
zant95
Invitado
 
Mensajes: n/a
Puntos:
Pregunta Potencias en JavaScript

Parecerá una duda un poco tonta ¿pero por qué si pongo "document.write(2^2)" me sale "0" y no "4"?

Sin embargo con "document.write(Math.pow(2,2))" si obtengo el resultado correcto.

¿Se podría crear alguna función para que cada vez que salga "x^y" se convierta automáticamente en "Math.pow(x,y)"?

Saludos.
  #2 (permalink)  
Antiguo 02/09/2010, 18:08
Avatar de maycolalvarez
Colaborador
 
Fecha de Ingreso: julio-2008
Ubicación: Caracas
Mensajes: 12.120
Antigüedad: 15 años, 9 meses
Puntos: 1532
Respuesta: Potencias en JavaScript

en js ^ corresponde a XOR
  #3 (permalink)  
Antiguo 02/09/2010, 18:12
Avatar de zerokilled
Javascripter
 
Fecha de Ingreso: abril-2009
Ubicación: Isla del Encanto, La Borinqueña [+>==]
Mensajes: 8.050
Antigüedad: 15 años
Puntos: 1485
Respuesta: Potencias en JavaScript

el operador de potencias no existe en javascript. como comenta maycol, en javascript el operador ^ es el operador bitwise XOR. como ya sabras, en javascript para calcular potencias se usa el metodo pow del objeto Math.
__________________
la maldad es una virtud humana,
y la espiritualidad es la lucha del hombre contra su maldad.
  #4 (permalink)  
Antiguo 02/09/2010, 18:37
zant95
Invitado
 
Mensajes: n/a
Puntos:
Respuesta: Potencias en JavaScript

Ok, muchas gracias. Es que estoy haciendo una calculadora con HTML, CSS y Javascript y me gustaría poner un botón para potencias.

Si alguien sabe como hacerlo se lo agradecería.

Aquí dejo el código:

Código HTML:
Ver original
  1. <?xml version="1.1" encoding="utf-8" ?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="sp">
  4. <title>Calculadora</title>
  5. <meta http-equiv="content-type" content="application/xhtml+xml; charset=utf-8" />
  6. <style type="text/css">
  7. body {
  8.   margin:0px;
  9.   text-align:center;
  10. }
  11.  
  12. #title {
  13.   font-family:arial,sans-serif;
  14.   font-size:13px;
  15.   color:#cccccc;
  16. }
  17.  
  18. #text {
  19.   background-color:#cccccc;
  20.   font-family:arial,sans-serif;
  21.   font-size:30px;
  22.   width:90%;
  23.   height:40px;
  24. }
  25.  
  26. #border1 {
  27.   background-color:#EEEEEE;
  28.   border-color: #aaaaaa;
  29.   border-width: 5px;
  30.   border-style: solid;
  31.   width:370px;
  32.   height:370px;
  33. }
  34.  
  35. #border2 {
  36.   background-color:#333333;
  37.   border-color: #aaaaaa;
  38.   border-width: 3px;
  39.   border-style: solid;
  40.   width:100%;
  41.   height:100%;
  42. }
  43.  
  44. .space {
  45.   margin:10px;
  46. }
  47.  
  48. .buttom {
  49.   border:1px dotted;
  50.   background-color:#6d6d6d;
  51.   font-family:arial,sans-serif;
  52.   font-size:25px;
  53.   color:#ffffff;
  54.   width:85px;
  55.   height:40px;
  56. }
  57.  
  58. .equal {
  59.   border:1px dotted;
  60.   background-color:#3d67a7;
  61.   font-family:arial,sans-serif;
  62.   font-size:25px;
  63.   color:#cccccc;
  64.   width:174px;
  65.   height:40px;
  66. }
  67.  
  68. .clean {
  69.   border:1px dotted;
  70.   background-color:#bd1d46;
  71.   font-family:arial,sans-serif;
  72.   font-size:25px;
  73.   color:#cccccc;
  74.   width:85px;
  75.   height:40px;
  76. }
  77.  
  78. .numbers {
  79.   border:1px dotted;
  80.   background-color:#3da03f;
  81.   font-family:arial,sans-serif;
  82.   font-size:25px;
  83.   color:#cccccc;
  84.   width:85px;
  85.   height:40px;
  86. }
  87.  
  88. .functions {
  89.   border:1px dotted;
  90.   background-color:#404040;
  91.   font-family:arial,sans-serif;
  92.   font-size:25px;
  93.   color:#cccccc;
  94.   width:85px;
  95.   height:40px;
  96. }
  97. <script type="text/javascript">
  98. function addChar(input, character)
  99. {
  100.     if(input.value == null || input.value == "0")
  101.         input.value = character
  102.     else
  103.         input.value += character
  104. }
  105.  
  106. function deleteChar(input)
  107. {
  108.     input.value = input.value.substring(0, input.value.length - 1)
  109. }
  110.  
  111. function changeSign(input)
  112. {
  113.     if(input.value.substring(0, 1) == "-")
  114.   input.value = input.value.substring(1, input.value.length)
  115.     else
  116.   input.value = "-" + input.value
  117. }
  118.  
  119. function compute(form)
  120. {
  121.   form.display.value = eval(form.display.value)
  122. }
  123.  
  124. function square(form)
  125. {
  126.   form.display.value = eval(form.display.value) * eval(form.display.value)
  127. }
  128.  
  129. function checkNum(str)
  130. {
  131.   for (var i = 0; i < str.length; i++) {
  132.    var ch = str.substring(i, i+1)
  133.    if (ch < "0" || ch > "9") {
  134.       if (ch != "/" && ch != "*" && ch != "^" && ch != "+" && ch != "-" && ch != "."
  135.        && ch != "(" && ch!= ")") {
  136.        alert("\u00A1Error!")
  137.        return false
  138.      }
  139.    }
  140.  }
  141.  return true
  142. }
  143. </script>
  144. </head>
  145. <body>
  146. <div>
  147. <form action="">
  148. <table id="border1">
  149. <tr>
  150. <td colspan="4">
  151.  
  152. <table id="border2">
  153. <tr>
  154. <td><p class="space"></p><input name="display" id="text" value="0" /><p class="space" ></p></td>
  155. </tr>
  156. </table>
  157.  
  158. </td>
  159. </tr>
  160.  
  161. <tr>
  162. <td>
  163. <input type="button" value="7" class="buttom"
  164.  onclick="addChar(this.form.display, '7')" />
  165. </td>
  166. <td>
  167. <input type="button" value="8" class="buttom"
  168.  onclick="addChar(this.form.display, '8')" />
  169. </td>
  170. <td>
  171. <input type="button" value="9" class="buttom"
  172.  onclick="addChar(this.form.display, '9')" />
  173. </td>
  174. <td>
  175. <input type="button" value="/" class="functions"
  176.  onclick="addChar(this.form.display, '/')" />
  177. </td>
  178. </tr>
  179.  
  180. <tr>
  181. <td>
  182. <input type="button" value="4" class="buttom"
  183.  onclick="addChar(this.form.display, '4')" />
  184. </td>
  185. <td>
  186. <input type="button" value="5" class="buttom"
  187.  onclick="addChar(this.form.display, '5')" />
  188. </td>
  189. <td>
  190. <input type="button" value="6" class="buttom"
  191.  onclick="addChar(this.form.display, '6')" />
  192. </td>
  193. <td>
  194. <input type="button" value="*" class="functions"
  195.  onclick="addChar(this.form.display, '*')" />
  196. </td>
  197. </tr>
  198.  
  199. <tr>
  200. <td>
  201. <input type="button" value="1" class="buttom"
  202.  onclick="addChar(this.form.display, '1')" />
  203. </td>
  204. <td>
  205. <input type="button" value="2" class="buttom"
  206.  onclick="addChar(this.form.display, '2')" />
  207. </td>
  208. <td>
  209. <input type="button" value="3" class="buttom"
  210.  onclick="addChar(this.form.display, '3')" />
  211. </td>
  212. <td>
  213. <input type="button" value="-"  class="functions"
  214.  onclick="addChar(this.form.display, '-')" />
  215. </td>
  216. </tr>
  217.  
  218. <tr>
  219. <td>
  220. <input type="button" value="0" class="buttom"
  221.  onclick="addChar(this.form.display, '0')" />
  222. </td>
  223. <td>
  224. <input type="button" value="." class="buttom"
  225.  onclick="addChar(this.form.display, '.')" />
  226. </td>
  227. <td>
  228. <input type="button" value="+/-" class="functions"
  229.  onclick="changeSign(this.form.display)" />
  230. </td>
  231. <td>
  232. <input type="button" value="+" class="functions"
  233.  onclick="addChar(this.form.display, '+')" />
  234. </td>
  235. </tr>
  236.  
  237. <tr>
  238. <td>
  239. <input type="button" value="(" class="functions"
  240.  onclick="addChar(this.form.display, '(')" />
  241. </td>
  242. <td>
  243. <input type="button" value=")" class="functions"
  244.  onclick="addChar(this.form.display, ')')" />
  245. </td>
  246. <td>
  247. <input type="button" value="^" class="functions"
  248.  onclick="addChar(this.form.display, '^')" />
  249. </td>
  250. <td>
  251. <input type="button" value="«" class="clean"
  252.   onclick="deleteChar(this.form.display)" />
  253. </td>
  254. </tr>
  255.  
  256. <tr>
  257. <td colspan="2">
  258. <input type="button" value="=" name="enter" class="equal"
  259.  onclick="if (checkNum(this.form.display.value))
  260.  { compute(this.form) }" />
  261. </td>
  262. <td>
  263. <select id="other" class="numbers" onchange="addChar(this.form.display,this.value);document.getElementById('other').options.selectedIndex = 0;">
  264. <option value="0">More</option>
  265. <option value="1.618033988749894">ϕ</option>
  266. <option value="2.718281828459045">e</option>
  267. <option value="3.141592653589793">π</option>
  268. </td>
  269. <td colspan="1">
  270. <input type="button" value="C" class="clean"
  271.  onclick="this.form.display.value = 0 " />
  272. </td>
  273. </tr>
  274. </form>
  275. </div>
  276. </body>
  277. </html>

Que se debería ver más o menos así (desde Google Chrome):



Saludos.

EDITO:

Ya lo he solucionado actualizando esta función:

Código Javascript:
Ver original
  1. function square(form)
  2. {
  3.   form.display.value = Math.pow(eval(form.display.value), prompt('Exponent:', '1'))
  4. }

Y cambiando esto otro:

Código HTML:
Ver original
  1. <td>
  2. <input type="button" value="^" class="functions"
  3.  onclick="square(this.form)" />
  4. </td>
  5. <td>

Saludos y gracias.

Última edición por zant95; 02/09/2010 a las 21:00

Etiquetas: Ninguno
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 02:18.