Note that there are some explanatory texts on larger screens.

plurals
  1. POjavascript wont allow me to set a variable to an operator( + - / *)
    text
    copied!<p>So I am trying to randomize whether I am adding or subtracting. right now in this code what should happen is when I navigate to the #second page the code will load a new Problem is created. Inside the Problem function is should get a random number 0 or 1 and then based on that number the variable symbol should be set to a plus sign or a minus sign. This is not occurring.</p> <p>So my question is: Can I set a variable to a javascript operator sign? </p> <p>In this code below it never writes to the screen because it doesnt like me to set a variable to a + - sign? code in question:</p> <pre><code>$(document).delegate("#second", "pageshow", function(){ var problem = new Problem; function Problem (){ var num = Math.floor(Math.random()*2); var symbol; if(num) symbol = +; else symbol = -; $(".random").html(num + " " + symbol); } }); </code></pre> <p>here is my html: </p> <pre><code>&lt;/script&gt; &lt;div data-role="header" data-position="fixed"&gt; &lt;h1&gt;Hello&lt;/h1&gt; &lt;/div&gt;&lt;!-- /header --&gt; &lt;div data-role="content" id="timer"&gt; &lt;a href="#main" data-role="button"&gt;main&lt;/a&gt; &lt;p class="random"&gt;&lt;/p&gt; &lt;/div&gt;&lt;!-- /content --&gt; &lt;div data-role="footer" data-position="fixed"&gt; &lt;h4&gt;Goodbye&lt;/h4&gt; &lt;/div&gt;&lt;!-- /footer --&gt; &lt;/div&gt;&lt;!-- /page --&gt; </code></pre> <p>sorry should have mentioned that I actually want to use the operators later therefore I need to be able to assign the + or - is such a way that I can then perform legal math operations with them later.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload