Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
| Search | Today's Posts | Mark Forums Read
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old April 9th, 2005, 06:56 AM
Friend of Wrox
 
Join Date: May 2004
Location: , , .
Posts: 212
Thanks: 0
Thanked 0 Times in 0 Posts
Default Randomly choose Operator from Array

Hi Guys,

I have an array that looks like this:

var theoperator = new array("<<", ">>", "^", "/", "+", "*")

Then I want to pass one of the operators into a routine that looks like this:

var thenum
var theseed = (Math.random() * 1000)
var theseed2 = (Math.random() / Math.random())
thenum =theseed theoperatorhere (Math.random() * theseed2) * 100000

Where it says 'theoperatorhere' is obviously where I would like to pass the operator to, randomly chosen from the 'theoperator' array. How do I do this?

Thanks in advance
interrupt
__________________
\'sync\' &lt;cr&gt;
The name specified is not recognized as an internal or external command, operable program or batch file.
  #2 (permalink)  
Old April 9th, 2005, 07:19 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

First get your "random" operator:
Code:
var arrOperators = new Array("<<", ">>", "^", "/", "+", "*");
var iRandom = Math.floor((Math.random() * arrOperators.length)); 
var sOperator = arrOperators[iRandom];
Then use eval to evaluate:
Code:
var result = eval("theseed theoperatorhere (Math.random() * theseed2) * 100000");
I don't like using eval but your alternative is just a huge switch statement.


--

Joe (Microsoft MVP - XML)
  #3 (permalink)  
Old April 9th, 2005, 07:49 AM
Friend of Wrox
 
Join Date: May 2004
Location: , , .
Posts: 212
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Joe,

thanks for your help, but I get an error here according to the debugger:

var sOperator = arrOperators[iRandom];

Error: Expected ';'.




Similar Threads
Thread Thread Starter Forum Replies Last Post
Session Variables Randomly Disappear? Ron Howerton VS.NET 2002/2003 106 July 26th, 2012 02:16 PM
Select nodes randomly carlosaml XSLT 3 October 17th, 2007 10:18 AM
Invalid operator for data type. Operator equals di Pusstiu SQL Server 2000 2 August 10th, 2007 04:51 AM
"Enter parameter value" occurrs randomly carrie09 Access 5 April 3rd, 2007 10:47 AM
Session Variables Randomly Disappeared Dmitriy General .NET 0 November 20th, 2006 08:42 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.