...

View Full Version : hellp needed in understaning this simple javascript code



haydar
05-06-2009, 01:32 AM
http://i163.photobucket.com/albums/t284/yoitsmehaydo/aaaaa.jpg

venegal
05-06-2009, 01:36 AM
Ah, I remember that, it's quite an old script -- 10 years ago we all used it.
It asks you to enter names, and if you do so, it does your homework for you.

haydar
05-06-2009, 01:57 AM
Ah, I remember that, it's quite an old script -- 10 years ago we all used it.
It asks you to enter names, and if you do so, it does your homework for you.

its not homework its exam revision. why does it even matter afteral what it is for. please dont leave silly comments.

venegal
05-06-2009, 02:14 AM
I'd gladly help if you asked a specific question about a part of the script you had trouble understanding, but just pasting in something that very much looks like a homework assignment is decidedly against the rules.

If it's exam revision, please post the answer you gave to that question in the exam. That will definitely help us find out how best to help you.

bdl
05-06-2009, 02:14 AM
its not homework its exam revision. why does it even matter afteral what it is for. please dont leave silly comments.

Don't be rude. What venegal is saying is that the forums are not for posting homework (or exam) problems for us to explain to you. If it's work you're supposed to be doing, then you need to figure it out. Afterall, it's probably the most simple script example you could ask for.

haydar
05-06-2009, 02:24 AM
please i dont want to agrue about what is what and this is that i just wonna understand the code. , venegal can you please tell me what is line 6 of that javascript code doing? where it starts with the if(next>..... im not to sure about what that lines doing.

venegal
05-06-2009, 03:04 AM
Alright, that's a fair enough question. I can see how one could have trouble understanding that. Actually, this whole script is bogus, but let's stick to the line you specified:

What it should do is validate the user input and, if successfully validated, write it into the names array.

What it does do is generate an error, because variables are case sensitive, and I has not been declared -- i has.

Now for the validation itself:
The condition rejects the input if it's either empty or consists of exactly one space, which doesn't make much sense, because when a user does not want to enter any more names, he will definitely not input a single space -- he will either input nothing, which results in an empty string, or he will hit cancel, which I guess may result in that "undefined" in the second condition, but more probably just in null.
It would be much better to just check the input for truth, because everything that will evaluate to false won't be a proper name anyway.

Now for the fun part:
That line is utterly unnecessary. It makes sure that nothing invalid goes into the array, but that's the exact same thing the while loop does anyway.

So all in all the whole error riddled and tiresome part before the document.writes start (which are complete bogus too!) could (and should!) be rewritten to something like


<script type="text/javascript">
var names = [];
while (name = window.prompt("Enter the Next Name")) names.push(name);

bdl
05-06-2009, 03:25 AM
What it does do is generate an error, because variables are case sensitive, and I has not been declared -- i has.


Actually, it doesn't; I is implicitly declared in the for..in construct as the iterative property of the object given. It should be declared using the var keyword, but then again so should i, next and names.

The line that should fail is this one:


Document.write("/OL>");

Two thing wrong with that, I'll leave that for you to fight over. :D

I agree with the rest of your post though. ;)

haydar
05-06-2009, 04:39 AM
Actually, it doesn't; I is implicitly declared in the for..in construct as the iterative property of the object given. It should be declared using the var keyword, but then again so should i, next and names.

The line that should fail is this one:


Document.write("/OL>");

Two thing wrong with that, I'll leave that for you to fight over. :D

I agree with the rest of your post though. ;)



Document.write("/OL>"); <----------- meant to be Document.write("</OL>");


and also <LI> tag is meant to be fore that??

Old Pedant
05-06-2009, 07:57 AM
bdl is wrong: The *FIRST* usage of I is incorrect.


names[I] = next;

*THAT* should be i instead of I

But bdl *did* correctly note that there are *TWO* (not one, TWO) things wrong with


Document.write("/OL>");

You only caught one of them.

And it's bad form to use <LI> without using a corresponding </LI>, but browsers *will* accept it.

********************

And I have to ask: WHAT in the world is "exam revision"????

Never heard the term used the way it seems to be used here.

Philip M
05-06-2009, 08:25 AM
And I have to ask: WHAT in the world is "exam revision"????

Never heard the term used the way it seems to be used here.

Exam revision usually means working through the questions asked in previous papers by way of preparation/revision.

bdl
05-06-2009, 03:13 PM
bdl is wrong: The *FIRST* usage of I is incorrect.


names[I] = next;

*THAT* should be i instead of I


You're absolutely right. Nice catch (to venegal as well, now that I see it). Just didn't look far enough up in the code. I was looking (obviously) at I var in the next loop. D'oh!

haydar
05-06-2009, 05:47 PM
for (i in names)<--------- thats not a vaild javascript for loop is it?

are the for loop in javascript same as for looks in c programming?

like they have for( int i=0; condition;incremedation)

venegal
05-06-2009, 06:39 PM
There's both. There's for (i = 0; i < iterations; i++) and there's for (key in array). As indicated by my variable names, the latter gives you the array key, not the array value. For arrays with consecutive indices starting with 0 it is equivalent to for(key = 0; key < array.length; i++).



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum