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

J

James Black

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.
 
J

James Black

Randy said:
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.
 
R

Randy Webb

James Black said the following on 5/21/2006 4:08 PM:
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.
 
R

Randy Webb

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

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:)
 
R

RobG

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

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.


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.
 
J

James Black

Randy said:
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.
 
R

Randy Webb

James Black said the following on 5/22/2006 7:12 AM:
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...e=UTF-8&group=comp.lang.javascript&scoring=d&>

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.

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?
 
J

James Black

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


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 =
 
R

Randy Webb

James Black said the following on 5/22/2006 1:58 PM:
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.
}
 
J

James Black

Randy said:
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.
 
R

Randy Webb

James Black said the following on 5/22/2006 7:17 PM:
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.....
 
R

RobG

Randy said:
James Black said the following on 5/22/2006 7:17 PM:

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.

[...]
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.
 
R

RobG

RobG said:
Randy said:
James Black said the following on 5/22/2006 7:17 PM:

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>'));

[...]
 
J

James Black

RobG said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top