473,378 Members | 1,617 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,378 software developers and data experts.

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

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.

May 21 '06 #1
15 1545
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/
May 21 '06 #2

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.

May 21 '06 #3
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/
May 21 '06 #4
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/
May 21 '06 #5
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/>
May 22 '06 #6

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.

May 22 '06 #7
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%2Fcom p.lang.javascript%2Fbrowse_frm%2Fthread%2Fe8d87fd3 95689d91%2F38548fc341a4e616%3Fhl%3Den%26lr%3D%26ie %3DUTF-8%26rnum%3D4%26prev%3D%2Fgroups%3Fq%3Ddynamically% 2Bload%2Bjs%2Bfile%2Bgroup:comp.lang.javascript%26 hl%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/
May 22 '06 #8

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 =

May 22 '06 #9
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/
May 22 '06 #10

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.

May 22 '06 #11
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/
May 23 '06 #12

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


Thank you, I will make the change.

Thank you for your help.

May 23 '06 #13
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/>
May 23 '06 #14
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/>
May 23 '06 #15

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.

May 24 '06 #16

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

Similar topics

8
by: JT | last post by:
i have written some asp that reads a fixed length text file, line by line, inserting each line into my database. my problem is that the text file format seems to have extra space at the end of the...
7
by: Voetleuce en fênsievry | last post by:
Hello everyone. I'm not a JavaScript author myself, but I'm looking for a method to remove duplicate words from a piece of text. This text would presumably be pasted into a text box. I have,...
8
by: Rich Grise | last post by:
I think I've finally found a tutorial that can get me started: http://www.zib.de/Visual/people/mueller/Course/Tutorial/tutorial.html and I've been lurking for awhile as well. What happened is,...
4
by: moondaddy | last post by:
I have a datagrid where a user can edit a number in one of the columns for a quantity value. On the postback I loop through all of the datagrid items and if the quantity has changed, then I update...
9
by: Merlin | last post by:
Hi, My code below doesn't work does anyone have any pointers? All my controls are programically added. Dim i As Int16 For i = 0 To Me.Controls.Count - 1 If Me.Controls(i).Name <>...
56
by: Zytan | last post by:
Obviously you can't just use a simple for loop, since you may skip over elements. You could modify the loop counter each time an element is deleted. But, the loop ending condition must be...
1
by: bdbeames | last post by:
I am currently getting some forecast data from a web site using (file_get_contents), then I'm using explode to put the string in an array, which I am then parsing. The trouble is when I use...
7
by: Qbert16 | last post by:
I'm having a lot of trouble with the following task for one of my assignments in python. Basically I need to take the user input and replace any words in the input and replace them with words from a...
20
by: Nates | last post by:
I have a .bas file saved locally that I load into my Acces project to run a particular sub. I use the following code to load the module (which works fine): I use the following loop to remove...
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
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.