trying to remove extra words from loop, such as 'each', 'all', 'collect'

Discussion in 'Javascript' started by James Black, May 21, 2006.

  1. James Black

    James Black Guest

    If you go to http://dante.acomp.usf.edu/HomeworkAssistant/index.php you
    will see my code.

    Type in:
    s = a + b and hit tab, and you will see the extra words.

    How do I remove these?

    Here is a snippet of my code:
    var myvalue = targ.value;
    if(myvalue.indexOf('=') > -1) {
    myvalue = myvalue.substring(myvalue.indexOf('=') + 1);
    }
    var elements = myvalue.split(' ');
    var lastvariable;
    var variables = [];
    var s;
    for(var t = 0; t < elements.length; t++) {
    s = elements[t];


    targ is the element of the input tag.

    s is the words in the array.

    Thank you for your help.
     
    James Black, May 21, 2006
    #1
    1. Advertising

  2. James Black

    Randy Webb Guest

    Re: trying to remove extra words from loop, such as 'each', 'all','collect'

    James Black said the following on 5/20/2006 11:42 PM:
    > If you go to http://dante.acomp.usf.edu/HomeworkAssistant/index.php you
    > will see my code.
    >
    > Type in:
    > s = a + b and hit tab, and you will see the extra words.


    All I see is a blank page with lots of errors.

    --
    Randy
    comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
    Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
     
    Randy Webb, May 21, 2006
    #2
    1. Advertising

  3. James Black

    James Black Guest

    Randy Webb wrote:

    > All I see is a blank page with lots of errors.


    Right now it only works with Firefox 1.5.

    I had forgotten to mention that, as I am using DOM2 in there, and
    that causes problems with IE6.
     
    James Black, May 21, 2006
    #3
  4. James Black

    Randy Webb Guest

    Re: trying to remove extra words from loop, such as 'each', 'all','collect'

    James Black said the following on 5/21/2006 4:08 PM:
    > Randy Webb wrote:
    >
    >> All I see is a blank page with lots of errors.

    >
    > Right now it only works with Firefox 1.5.


    Blank page with errors in 1.5.0.3

    Homework is not defined

    Reloading the page a second time gets it to semi-work.

    Here is 99% of your problems:

    insertScriptTags("prototype.js");
    insertScriptTags("scriptaculous.js");

    What exactly is that page trying to do though?

    > I had forgotten to mention that, as I am using DOM2 in there, and
    > that causes problems with IE6.


    Good thing I didn't try to see it in IE6 then huh?

    The question I got was:

    A car is traveling north at 100 mph toward a train. The train is moving
    east at 5mpg. The train is 880 yards long. The car needs to travel 3000
    m to hit the train. Where does the car hit the train?

    The answer is: In the front.

    --
    Randy
    comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
    Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
     
    Randy Webb, May 21, 2006
    #4
  5. James Black

    Randy Webb Guest

    Re: trying to remove extra words from loop, such as 'each', 'all','collect'

    Randy Webb said the following on 5/21/2006 5:44 PM:

    <snip>

    > The question I got was:
    >
    > A car is traveling north at 100 mph toward a train. The train is moving
    > east at 5mpg. The train is 880 yards long. The car needs to travel 3000
    > m to hit the train. Where does the car hit the train?
    >
    > The answer is: In the front.
    >


    Although "In the side" would be a better answer:)

    --
    Randy
    comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
    Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
     
    Randy Webb, May 21, 2006
    #5
  6. James Black

    RobG Guest

    Re: trying to remove extra words from loop, such as 'each', 'all','collect'

    Randy Webb wrote:
    > Randy Webb said the following on 5/21/2006 5:44 PM:
    >
    > <snip>
    >
    >> The question I got was:
    >>
    >> A car is traveling north at 100 mph toward a train. The train is
    >> moving east at 5mpg. The train is 880 yards long. The car needs to


    It is unusual to define velocity in terms normally associated with fuel
    consumption - or does "mpg" stand for something other than "miles per
    gallon"? Are they US or Imperial gallons?

    I guess that's a rhetorical question since the US is the only country in
    the world that still uses gallons.


    >> travel 3000 m to hit the train. Where does the car hit the train?
    >>
    >> The answer is: In the front.


    If the car is traveling forwards, that is possible, though I think you
    mean "with its front". It might be traveling in reverse, in which case
    it will hit the train with its rear. ;-)

    "On the right side" or "on the southern side" might be correct if indeed
    the car does hit the train.


    > Although "In the side" would be a better answer:)


    Any train "moving east at 5mpg" is consuming very little fuel in terms
    of a locomotive 880 yards (804 metres) long and so is probably rolling
    at some indeterminate (though likely very fast) speed with the engine
    idling.

    There is no starting reference given for the train, therefore the car
    might hit the train anywhere along its length, if it hits it at all. If
    the train driver works it just right, the train might hit the car.


    --
    Rob
    Group FAQ: <URL:http://www.jibbering.com/faq/>
     
    RobG, May 22, 2006
    #6
  7. James Black

    James Black Guest

    Randy Webb wrote:
    > Reloading the page a second time gets it to semi-work.
    >
    > Here is 99% of your problems:
    >
    > insertScriptTags("prototype.js");
    > insertScriptTags("scriptaculous.js");
    >
    > What exactly is that page trying to do though?


    See what problems I get if I try to dynamically load script files.
    This is mainly to see if I can later use it to load a drawing api, if
    the client doesn't support svg or the canvas tag.

    I am also just playing with what is the minimum I could have in a
    page and still get it to work.


    > A car is traveling north at 100 mph toward a train. The train is moving
    > east at 5mpg. The train is 880 yards long. The car needs to travel 3000
    > m to hit the train. Where does the car hit the train?
    >
    > The answer is: In the front.


    The question is pointless, and I could have put anything, as getting
    the correct question is a later issue, since I will use ajax for that.

    The part I am concerned with right now is properly handling
    equations, which is why, in my initial post here I suggested that you
    type in:
    s = a + b

    That leads to the problem I am curious about.
     
    James Black, May 22, 2006
    #7
  8. James Black

    Randy Webb Guest

    Re: trying to remove extra words from loop, such as 'each', 'all','collect'

    James Black said the following on 5/22/2006 7:12 AM:
    > Randy Webb wrote:
    >> Reloading the page a second time gets it to semi-work.
    >>
    >> Here is 99% of your problems:
    >>
    >> insertScriptTags("prototype.js");
    >> insertScriptTags("scriptaculous.js");
    >>
    >> What exactly is that page trying to do though?

    >
    > See what problems I get if I try to dynamically load script files.


    That wasn't the question you asked though. A better question, to get the
    answer you are hunting, might have been:

    "What possible problems might I encounter in trying to dynamically load
    script files?"

    > This is mainly to see if I can later use it to load a drawing api, if
    > the client doesn't support svg or the canvas tag.


    <URL:
    http://groups.google.com/group/comp.lang.javascript/tree/browse_frm/thread/e8d87fd395689d91/38548fc341a4e616?rnum=1&hl=en&_done=%2Fgroup%2Fcomp.lang.javascript%2Fbrowse_frm%2Fthread%2Fe8d87fd395689d91%2F38548fc341a4e616%3Fhl%3Den%26lr%3D%26ie%3DUTF-8%26rnum%3D4%26prev%3D%2Fgroups%3Fq%3Ddynamically%2Bload%2Bjs%2Bfile%2Bgroup:comp.lang.javascript%26hl%3Den%26lr%3D%26ie%3DUTF-8%26group%3Dcomp.lang.javascript%26scoring%3Dd%26>

    Happy Reading :)

    > I am also just playing with what is the minimum I could have in a
    > page and still get it to work.


    function loadJSFile(fileURL){
    var loadScript = document.createElement('script');
    loadScript.type = "text/javascript";
    loadScript.src = fileURL;
    document.getElementsByTagName('head')[0].appendChild(loadScript);
    }

    and then call it like this:

    loadJSFile('URL to .js File');

    That is basics for most modern browsers. For browsers that do not
    support getElementsByTagName, appendChild, or createElement, there are
    possible alternatives, depending on the browser.


    >
    >> A car is traveling north at 100 mph toward a train. The train is moving
    >> east at 5mpg. The train is 880 yards long. The car needs to travel 3000
    >> m to hit the train. Where does the car hit the train?
    >>
    >> The answer is: In the front.

    >
    > The question is pointless, and I could have put anything, as getting
    > the correct question is a later issue, since I will use ajax for that.


    Use AJAX to get a .js file from the server?

    But my answer to the question was purely in fun.

    > The part I am concerned with right now is properly handling
    > equations, which is why, in my initial post here I suggested that you
    > type in:
    > s = a + b


    Yet you still have not told what you think/want it to do when someone
    does that.

    > That leads to the problem I am curious about.


    And I asked what that problem was by asking what the page is supposed to
    do other than "properly handling equations". What do you want it to do?

    --
    Randy
    comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
    Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
     
    Randy Webb, May 22, 2006
    #8
  9. James Black

    James Black Guest

    Randy Webb wrote:

    > > The part I am concerned with right now is properly handling
    > > equations, which is why, in my initial post here I suggested that you
    > > type in:
    > > s = a + b

    >
    > Yet you still have not told what you think/want it to do when someone
    > does that.
    >
    > > That leads to the problem I am curious about.

    >
    > And I asked what that problem was by asking what the page is supposed to
    > do other than "properly handling equations". What do you want it to do?


    It should create 2 more input boxes:
    a = ____________
    b = ____________

    where the underlines are input boxes.

    But, I get the following, with input boxes after the equal sign. I
    don't know why I get the other objects as the array I use to print this
    has a length of 2.

    Thank you for your response, so far, btw. Sorry I was not very clear.

    a =
    b =
    each =
    all =
    any =
    collect =
    detect =
    findAll =
    grep =
    include =
    inject =
    invoke =
    max =
    min =
    partition =
    pluck =
    reject =
    sortBy =
    toArray =
    zip =
    inspect =
    find =
    select =
    member =
    entries =
    _reverse =
    _each =
    clear =
    first =
    last =
    compact =
    flatten =
    without =
    call =
     
    James Black, May 22, 2006
    #9
  10. James Black

    Randy Webb Guest

    Re: trying to remove extra words from loop, such as 'each', 'all','collect'

    James Black said the following on 5/22/2006 1:58 PM:
    > Randy Webb wrote:
    >
    >>> The part I am concerned with right now is properly handling
    >>> equations, which is why, in my initial post here I suggested that you
    >>> type in:
    >>> s = a + b

    >> Yet you still have not told what you think/want it to do when someone
    >> does that.
    >>
    >>> That leads to the problem I am curious about.

    >> And I asked what that problem was by asking what the page is supposed to
    >> do other than "properly handling equations". What do you want it to do?

    >
    > It should create 2 more input boxes:
    > a = ____________
    > b = ____________
    >
    > where the underlines are input boxes.
    >
    > But, I get the following, with input boxes after the equal sign. I
    > don't know why I get the other objects as the array I use to print this
    > has a length of 2.


    Probably because you are using a for-in loop instead of a normal for loop.

    for (i in myArray){
    //do something with i
    }

    instead of:

    for (var = 0;i<myArray.length;i++){
    //process myArray contents.
    }

    --
    Randy
    comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
    Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
     
    Randy Webb, May 22, 2006
    #10
  11. James Black

    James Black Guest

    Randy Webb wrote:
    > Probably because you are using a for-in loop instead of a normal for loop.
    >
    > for (i in myArray){
    > //do something with i
    > }
    >
    > instead of:
    >
    > for (var = 0;i<myArray.length;i++){
    > //process myArray contents.
    > }


    But it should be able to work with a for in loop, I thought.

    I am using an associative array, which is why I am not using the
    regular for loop, as, I haven't figured out how to get a regular loop
    to work with these types of arrays.
     
    James Black, May 23, 2006
    #11
  12. James Black

    Randy Webb Guest

    Re: trying to remove extra words from loop, such as 'each', 'all','collect'

    James Black said the following on 5/22/2006 7:17 PM:
    > Randy Webb wrote:
    >> Probably because you are using a for-in loop instead of a normal for loop.
    >>
    >> for (i in myArray){
    >> //do something with i
    >> }
    >>
    >> instead of:
    >>
    >> for (var = 0;i<myArray.length;i++){
    >> //process myArray contents.
    >> }

    >
    > But it should be able to work with a for in loop, I thought.


    No. Because for-in picks up all properties of the Object. Not just the
    ones you created.

    > I am using an associative array,


    You may be using something that behaves similar to an associative array
    but you are not using an associative array because Javascript doesn't
    have an associative array.

    > which is why I am not using the regular for loop, as, I haven't figured
    > out how to get a regular loop to work with these types of arrays.


    Then use a normal indexed array.....
    --
    Randy
    comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
    Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
     
    Randy Webb, May 23, 2006
    #12
  13. James Black

    James Black Guest

    Randy Webb wrote:
    > Then use a normal indexed array.....


    Thank you, I will make the change.

    Thank you for your help.
     
    James Black, May 23, 2006
    #13
  14. James Black

    RobG Guest

    Re: trying to remove extra words from loop, such as 'each', 'all','collect'

    Randy Webb wrote:
    > James Black said the following on 5/22/2006 7:17 PM:
    >> Randy Webb wrote:
    >>> Probably because you are using a for-in loop instead of a normal for
    >>> loop.
    >>>
    >>> for (i in myArray){
    >>> //do something with i
    >>> }
    >>>
    >>> instead of:
    >>>
    >>> for (var = 0;i<myArray.length;i++){
    >>> //process myArray contents.
    >>> }

    >>
    >> But it should be able to work with a for in loop, I thought.

    >
    > No. Because for-in picks up all properties of the Object. Not just the
    > ones you created.


    It *would* have worked if the OP hadn't messed with prototype.js.

    For..in picks up enumerable properties, all the standard array
    properties are not enumerable, try it:

    var a = [];
    var t = [];
    for (var prop in a){
    t.push(prop + ': ' + a[prop]);
    }
    document.write(t.join('<br>'));

    The all you get is:

    0: blah

    because all the other properties are DontEnum.

    As you noted previously, the real problem is the OP's use prototype.js,
    which adds properties to the array object. And since it's impossible to
    add properties as DontEnum, they are enumerable.

    [...]
    >
    >> which is why I am not using the regular for loop, as, I haven't
    >> figured out how to get a regular loop to work with these types of arrays.

    >
    > Then use a normal indexed array.....


    Or ditch prototype.js. :)

    The use of for..in is generally indicated if an array's length is large,
    it is also sparse and any extra properties can be accounted for. That
    doesn't seem to be the case here.


    --
    Rob
    Group FAQ: <URL:http://www.jibbering.com/faq/>
     
    RobG, May 23, 2006
    #14
  15. James Black

    RobG Guest

    Re: trying to remove extra words from loop, such as 'each', 'all','collect'

    RobG wrote:
    > Randy Webb wrote:
    >> James Black said the following on 5/22/2006 7:17 PM:
    >>> Randy Webb wrote:
    >>>> Probably because you are using a for-in loop instead of a normal for
    >>>> loop.
    >>>>
    >>>> for (i in myArray){
    >>>> //do something with i
    >>>> }
    >>>>
    >>>> instead of:
    >>>>
    >>>> for (var = 0;i<myArray.length;i++){
    >>>> //process myArray contents.
    >>>> }
    >>>
    >>> But it should be able to work with a for in loop, I thought.

    >>
    >> No. Because for-in picks up all properties of the Object. Not just the
    >> ones you created.

    >
    > It *would* have worked if the OP hadn't messed with prototype.js.
    >
    > For..in picks up enumerable properties, all the standard array
    > properties are not enumerable, try it:
    >
    > var a = [];
    > var t = [];
    > for (var prop in a){
    > t.push(prop + ': ' + a[prop]);
    > }
    > document.write(t.join('<br>'));
    >
    > The all you get is:
    >
    > 0: blah


    Ooops, messed with it:

    var a = ['blah'];
    var t = [];
    for (var prop in a){
    t.push(prop + ': ' + a[prop]);
    }
    document.write(t.join('<br>'));

    [...]

    --
    Rob
    Group FAQ: <URL:http://www.jibbering.com/faq/>
     
    RobG, May 23, 2006
    #15
  16. James Black

    James Black Guest

    RobG wrote:

    > The use of for..in is generally indicated if an array's length is large,
    > it is also sparse and any extra properties can be accounted for. That
    > doesn't seem to be the case here.


    I just turned it in an array of arrays, and it works fine now.

    I didn't realize that prototype.js was part of the problem.

    Thank you for helping me out.
     
    James Black, May 24, 2006
    #16
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. BerlinBrown
    Replies:
    6
    Views:
    4,838
  2. Replies:
    4
    Views:
    204
    Christophe Grandsire
    Oct 28, 2005
  3. Replies:
    5
    Views:
    145
  4. Isaac Won
    Replies:
    9
    Views:
    451
    Ulrich Eckhardt
    Mar 4, 2013
  5. Comment Holder
    Replies:
    10
    Views:
    134
    Chris Angelico
    Aug 22, 2013
Loading...

Share This Page