Javascript (Help me!!!!)

December 29, 2010 at 09:07:55
Specs: Windows XP
Here is my code:
<code>
<SCRIPT LANGUAGE="JAVASCRIPT">
//Script by Hello1234567890
var OperationsP = prompt('Which mathematical operation would you like to use?','(Adding,Subtracting,Multiplying,Dividing)');
var Operations = Adding & Subtracting & Multiplying & Dividing;
if (OperationsP!==Operations)
{
alert("You may only do four operations: Adding, Subtracting, Multiplying, Dividing. Also, please spell them correctly.")
}
</SCRIPT>
</code>

How do I make multiple things for var Operations?


See More: Javascript (Help me!!!!)

Report •

#1
December 29, 2010 at 12:31:15
I misread things quite a bit, so forgive me if I've botched what you're wanting to do. :D I *think* you're asking the user the enter operators, which could be one or perhaps all four? If so, try something like

<script type="text/javascript">

   OP_ADD = 1;
   OP_SUB = 2;
   OP_MUL = 4;
   OP_DIV = 8;

   var op = prompt("Addition, Subtraction, Multiplication, Division");
   var ops = 0;

   if(op.toLowerCase().indexOf("add") != -1) { ops += 1; }
   if(op.toLowerCase().indexOf("sub") != -1) { ops += 2; }
   if(op.toLowerCase().indexOf("mul") != -1) { ops += 4; }
   if(op.toLowerCase().indexOf("div") != -1) { ops += 8; }   

   switch(ops & (OP_ADD | OP_SUB | OP_MUL | OP_DIV)) {

      case  1  : alert(op + '\n' + 'add operator'); break;
      case  2  : alert(op + '\n' + 'sub operator'); break;
      case  3  : alert(op + '\n' + 'add, sub operators'); break;
      case  4  : alert(op + '\n' + 'mul operator'); break;
      case  5  : alert(op + '\n' + 'add, mul operators'); break;
      case  6  : alert(op + '\n' + 'sub, mul operators'); break;
      case  7  : alert(op + '\n' + 'add, sub, mul operators'); break;
      case  8  : alert(op + '\n' + 'div operator'); break;
      case  9  : alert(op + '\n' + 'add, div operators'); break;
      case 10  : alert(op + '\n' + 'sub, div operators'); break;
      case 11  : alert(op + '\n' + 'add, sub, div operators'); break;
      case 12  : alert(op + '\n' + 'mul, div operators'); break;
      case 13  : alert(op + '\n' + 'add, mul, div operators'); break;
      case 14  : alert(op + '\n' + 'sub, mul, div operators'); break;
      case 15  : alert(op + '\n' + 'all operators'); break;
      default  : alert(op + '\n' + 'invalid operator'); break;
   }
</script>

Not really tested or error proof, but hopefully, if it's what you're asking, it will work for you need.


Report •
Related Solutions


Ask Question