Help with indexOf()

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jim Davidson

    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_ImgN o.indexOf(strIm gNo) = -1)
    {
    string_of_ImgNo = string_of_ImgNo +strImgNo;
    break;
    }
    if (string_of_ImgN o.length > 9) break;
    return nImgNo
    }
    }
  • Janwillem Borleffs

    #2
    Re: Help with indexOf()


    "Jim Davidson" <raccoon@icubed .com> schreef in bericht
    news:eaa6451a.0 309161142.2ed70 bd8@posting.goo gle.com...[color=blue]
    > I'm teaching myself javascript and need some help with this code. can
    > someone tell me what's wrong with it?[/color]
    ....[color=blue]
    >
    > if (string_of_ImgN o.indexOf(strIm gNo) = -1)[/color]

    '=' is an assignment, when you want to do a comparision use '==':
    if (string_of_ImgN o.indexOf(strIm gNo) == -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



    Comment

    • Vjekoslav Begovic

      #3
      Re: Help with indexOf()

      "Jim Davidson" <raccoon@icubed .com> wrote in message
      news:eaa6451a.0 309161142.2ed70 bd8@posting.goo gle.com...[color=blue]
      > 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_ImgN o.indexOf(strIm gNo) = -1)[/color]

      And that is always true.

      Equality operator in Javascript is ==.



      Comment

      • Lee

        #4
        Re: Help with indexOf()

        Jim Davidson said:[color=blue]
        >
        >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_ImgN o.indexOf(strIm gNo) = -1)
        > {
        > string_of_ImgNo = string_of_ImgNo +strImgNo;
        > break;
        > }
        > if (string_of_ImgN o.length > 9) break;
        > return nImgNo
        > }
        >}[/color]

        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<1 0)
        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.

        Comment

        • Lasse Reichstein Nielsen

          #5
          Re: Help with indexOf()

          "Janwillem Borleffs" <jwb@jwbfoto.de mon.nl> writes:
          [color=blue]
          > When the function is stripped down, you will end up with something
          > like this:
          >
          > function getRandomNo() {
          > return Math.round(Math .random() * 10);
          > }[/color]

          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 - lrn@hotpop.com
          Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
          'Faith without judgement merely degrades the spirit divine.'

          Comment

          • Jim Davidson

            #6
            Re: Help with indexOf()

            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 <REM0VElbspamtr ap@cox.net> wrote in message news:<bk7qqu02j 3o@drn.newsguy. com>...[color=blue]
            > Jim Davidson said:[color=green]
            > >
            > >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_ImgN o.indexOf(strIm gNo) = -1)
            > > {
            > > string_of_ImgNo = string_of_ImgNo +strImgNo;
            > > break;
            > > }
            > > if (string_of_ImgN o.length > 9) break;
            > > return nImgNo
            > > }
            > >}[/color]
            >
            > 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<1 0)
            > 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.[/color]

            Comment

            • Dr John Stockton

              #7
              Re: Help with indexOf()

              JRS: In article <eaa6451a.03091 61142.2ed70bd8@ posting.google. com>, seen
              in news:comp.lang. javascript, Jim Davidson <raccoon@icubed .com> posted
              at Tue, 16 Sep 2003 12:42:08 :-
              [color=blue]
              >I'm teaching myself javascript and need some help with this code. can
              >someone tell me what's wrong with it?[/color]

              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.writel n(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.demo n.co.uk/js-index.htm> JS maths, dates, sources.
              <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.

              Comment

              Working...