473,322 Members | 1,538 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Help with indexOf()

I'm teaching myself javascript and need some help with this code. can
someone tell me what's wrong with it?
function getRandomNo()
{
while (true)
{
nImgNo = Math.round(Math.random() * 10);
var strImgNo = ""+nImgNo;

if (string_of_ImgNo.indexOf(strImgNo) = -1)
{
string_of_ImgNo = string_of_ImgNo+strImgNo;
break;
}
if (string_of_ImgNo.length > 9) break;
return nImgNo
}
}
Jul 20 '05 #1
6 3292

"Jim Davidson" <ra*****@icubed.com> schreef in bericht
news:ea**************************@posting.google.c om...
I'm teaching myself javascript and need some help with this code. can
someone tell me what's wrong with it? ....
if (string_of_ImgNo.indexOf(strImgNo) = -1)


'=' is an assignment, when you want to do a comparision use '==':
if (string_of_ImgNo.indexOf(strImgNo) == -1)

Also, string_of_ImgNo is undefined at this point, you should use strImgNo
instead when you really want to perform a string validation. However, this
isn't nessecary at all and you don't need to use the while loop either. When
the function is stripped down, you will end up with something like this:

function getRandomNo() {
return Math.round(Math.random() * 10);
}
JW

Jul 20 '05 #2
"Jim Davidson" <ra*****@icubed.com> wrote in message
news:ea**************************@posting.google.c om...
I'm teaching myself javascript and need some help with this code. can
someone tell me what's wrong with it?
function getRandomNo()
{
while (true)
{
nImgNo = Math.round(Math.random() * 10);
var strImgNo = ""+nImgNo;

if (string_of_ImgNo.indexOf(strImgNo) = -1)


And that is always true.

Equality operator in Javascript is ==.

Jul 20 '05 #3
Lee
Jim Davidson said:

I'm teaching myself javascript and need some help with this code. can
someone tell me what's wrong with it?
function getRandomNo()
{
while (true)
{
nImgNo = Math.round(Math.random() * 10);
var strImgNo = ""+nImgNo;

if (string_of_ImgNo.indexOf(strImgNo) = -1)
{
string_of_ImgNo = string_of_ImgNo+strImgNo;
break;
}
if (string_of_ImgNo.length > 9) break;
return nImgNo
}
}


1. You don't have a starting value for string_of_ImgNo.
2. You should never use Math.round() to generate random numbers [note 1].
Use Math.floor(), instead.
3. You don't need to convert numbers to strings. JavaScript doesn't care.
4. The comparison operator is "==", not "=".
5. You don't want to return nImgNo. You want string_of_ImgNo.
6. You don't want to "break" out of the loop until you're ready to
return, which means that you never want to use "break", at all.
Simply return when you're done, or better yet...
7. Change your while
loop condition to:
while(string_of_ImgNo.length<10)
and simply return string_of_ImgNo after the loop exits.
8. This isn't a very efficient way to generate a list of random
digits, anyway, but it will work if you fix the errors.
Note 1: Unless you're really sure you understand when it's ok.
In this case, using Math.round() will very often result in the
number "10" being added to your string, which would appear as
an extra 1 or 0 instead of some other digit, or possibly in
addition to all other digits, for a total of 11 digits.

Jul 20 '05 #4
"Janwillem Borleffs" <jw*@jwbfoto.demon.nl> writes:
When the function is stripped down, you will end up with something
like this:

function getRandomNo() {
return Math.round(Math.random() * 10);
}


And even then, you probably want either
return Math.floor(Math.random() * 10);
or
return Math.floor(Math.random() * 11);

The first gives a number between 0 and 9 with equal chance, the second
between 0 and 10 with equal chance. Using Math.round gives a number
between 0 and 10, with 0 and 10 having half the chance of the other
numbers.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #5
Well I see I still have a lot to learn, thanks for the help...by the
way string_of_ImgNo is a Global variable defined earlier. Thanks
again. I'm sure I'll be asking more newbie questions.

Lee <RE**************@cox.net> wrote in message news:<bk*********@drn.newsguy.com>...
Jim Davidson said:

I'm teaching myself javascript and need some help with this code. can
someone tell me what's wrong with it?
function getRandomNo()
{
while (true)
{
nImgNo = Math.round(Math.random() * 10);
var strImgNo = ""+nImgNo;

if (string_of_ImgNo.indexOf(strImgNo) = -1)
{
string_of_ImgNo = string_of_ImgNo+strImgNo;
break;
}
if (string_of_ImgNo.length > 9) break;
return nImgNo
}
}


1. You don't have a starting value for string_of_ImgNo.
2. You should never use Math.round() to generate random numbers [note 1].
Use Math.floor(), instead.
3. You don't need to convert numbers to strings. JavaScript doesn't care.
4. The comparison operator is "==", not "=".
5. You don't want to return nImgNo. You want string_of_ImgNo.
6. You don't want to "break" out of the loop until you're ready to
return, which means that you never want to use "break", at all.
Simply return when you're done, or better yet...
7. Change your while
loop condition to:
while(string_of_ImgNo.length<10)
and simply return string_of_ImgNo after the loop exits.
8. This isn't a very efficient way to generate a list of random
digits, anyway, but it will work if you fix the errors.
Note 1: Unless you're really sure you understand when it's ok.
In this case, using Math.round() will very often result in the
number "10" being added to your string, which would appear as
an extra 1 or 0 instead of some other digit, or possibly in
addition to all other digits, for a total of 11 digits.

Jul 20 '05 #6
JRS: In article <ea**************************@posting.google.com >, seen
in news:comp.lang.javascript, Jim Davidson <ra*****@icubed.com> posted
at Tue, 16 Sep 2003 12:42:08 :-
I'm teaching myself javascript and need some help with this code. can
someone tell me what's wrong with it?


It is possible to tell you some of what is wrong with it; for example,
string_of_ImgNo is undefined at the point of use, although that only
really matters if you intend to call the function getRandomNo.

But, as you have not said what you might want it to do, we cannot tell
you all that may be wrong with it, unless we guess the purpose.

My guess is that you want to return a ten-character string which is a
random permutation of '0123456789'; or that you want to return a digit
not previously used, which can be done by indexing into such a pre-
prepared permutation.

So you should read the newsgroup FAQ, searching for information on the
use of Random; the apparent problem is equivalent to dealing a deck of
ten cards 0..9. You will find a link to <http://www.merlyn.demon.co.uk/
js-randm.htm>.

Consider :

var RA = Deal(10), N=0, S=''

function Next() { return RA[N++] }

for (j=0; j<12; j++) S += Next()
document.writeln(RA, ' ',S)

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
Jul 20 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Sue | last post by:
<html> Is there someone here that can help me validate the period as the fourth from the last character in an email address. There is other information and validation on the form I have to do but...
5
by: Mike | last post by:
I'm using a script provided by e-mailanywhere, it's a little too big for me. There's 1 text field and 1 password field in a form. OnSubmit, I would like both fields to be validated to look for...
1
by: TAM | last post by:
Hi, I have a simple JavaScript code that ensures that all the form fields are filled and there is also a function that checks if the email is a valid address. For some reason IE is giving...
22
by: Rich | last post by:
I am trying to create a site that will re-direct a user based on their OS. For this site I will need to send NT4 and 95 users to site A and 2000/XP users to site B. All others should be directed...
4
by: Tressa | last post by:
I have a messagebox that I only want to pop up only if it is not already being displayed on the screen. My code is still poping up the messagebox even though it is on the screen. What am I doing...
2
by: mallard134 | last post by:
Could someone please help a newbee vb programmer with a question that is driving me crazy. I am trying to understand a line of code that is supposed to return the domain portion of a valid email...
1
YenRaven
by: YenRaven | last post by:
I have wrote a script that searches through the body's innerHTML to find and highlight each instance string omiting any matches in html tags. it works.. but slowly to the point that if you have more...
3
by: TC | last post by:
Hey All, I have some classes that I recently built for a website which uses the HttpWebRequest & HttpWebResponse objects from the System.Net namespace. Basically, the classes rap submitted data...
5
by: mcfly1204 | last post by:
I am attempting to use WebRequest to access a page that requires a login/password to access. My last WebRequest continues to timeout. Any help or thoughts would be appreciated. namespace...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.