Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 14 of 14
  1. #1
    New Coder
    Join Date
    May 2009
    Posts
    21
    Thanks
    6
    Thanked 0 Times in 0 Posts

    hellp needed in understaning this simple javascript code


  • #2
    GŁtkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    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.

  • Users who have thanked venegal for this post:

    haydar (05-06-2009)

  • #3
    New Coder
    Join Date
    May 2009
    Posts
    21
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by venegal View Post
    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.

  • #4
    GŁtkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    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.

  • Users who have thanked venegal for this post:

    haydar (05-06-2009)

  • #5
    bdl
    bdl is offline
    Regular Coder
    Join Date
    Apr 2007
    Location
    Camarillo, CA US
    Posts
    590
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Quote Originally Posted by haydar View Post
    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.

  • #6
    New Coder
    Join Date
    May 2009
    Posts
    21
    Thanks
    6
    Thanked 0 Times in 0 Posts
    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.

  • #7
    GŁtkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    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

    Code:
    <script type="text/javascript">
    var names = [];
    while (name = window.prompt("Enter the Next Name")) names.push(name);
    Last edited by venegal; 05-06-2009 at 03:08 AM.

  • Users who have thanked venegal for this post:

    haydar (05-06-2009)

  • #8
    bdl
    bdl is offline
    Regular Coder
    Join Date
    Apr 2007
    Location
    Camarillo, CA US
    Posts
    590
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Quote Originally Posted by venegal View Post
    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:
    Code:
    Document.write("/OL>");
    Two thing wrong with that, I'll leave that for you to fight over.

    I agree with the rest of your post though.
    Last edited by bdl; 05-06-2009 at 03:28 AM.

  • Users who have thanked bdl for this post:

    haydar (05-06-2009)

  • #9
    New Coder
    Join Date
    May 2009
    Posts
    21
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by bdl View Post
    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:
    Code:
    Document.write("/OL>");
    Two thing wrong with that, I'll leave that for you to fight over.

    I agree with the rest of your post though.
    Code:
    Document.write("/OL>"); <----------- meant to be Document.write("</OL>");
    and also <LI> tag is meant to be fore that??

  • #10
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,125
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    bdl is wrong: The *FIRST* usage of I is incorrect.
    Code:
    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
    Code:
    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.

  • #11
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,908
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by Old Pedant View Post
    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.

  • #12
    bdl
    bdl is offline
    Regular Coder
    Join Date
    Apr 2007
    Location
    Camarillo, CA US
    Posts
    590
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Quote Originally Posted by Old Pedant View Post
    bdl is wrong: The *FIRST* usage of I is incorrect.
    Code:
    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!

  • #13
    New Coder
    Join Date
    May 2009
    Posts
    21
    Thanks
    6
    Thanked 0 Times in 0 Posts
    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)

  • #14
    GŁtkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    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++).
    Last edited by venegal; 05-06-2009 at 06:43 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •