Need a quick debug, please

  • mindfullsilence
  • Professor
  • Professor
  • User avatar
  • Joined: Aug 04, 2008
  • Posts: 846
  • Status: Offline

Post January 21st, 2011, 4:03 pm

Anyone know why this isn't outputting anything? Prompt shows up once, which makes me think it's the while loop.
JAVASCRIPT Code: [ Select ]
function getNames()
{
var names=new Array(4);
var i = 0
var name = prompt("Please enter a name:","");
while(names[4] = '') {
names[i] = name;
var list = document.createElement('<li>'+names[i]+'<li>')
document.getElementById('nameList').appendChild(list)
i=i++
}
}
 
  1. function getNames()
  2. {
  3. var names=new Array(4);
  4. var i = 0
  5. var name = prompt("Please enter a name:","");
  6. while(names[4] = '') {
  7. names[i] = name;
  8. var list = document.createElement('<li>'+names[i]+'<li>')
  9. document.getElementById('nameList').appendChild(list)
  10. i=i++
  11. }
  12. }
  13.  

I'm calling the function through a body onload event.
Use your words like arrows to shoot toward your goal.
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post January 21st, 2011, 4:03 pm

  • mindfullsilence
  • Professor
  • Professor
  • User avatar
  • Joined: Aug 04, 2008
  • Posts: 846
  • Status: Offline

Post January 21st, 2011, 4:19 pm

Nevermind, got it.

JAVASCRIPT Code: [ Select ]
function getNames()
{
   // set up the array
var names=new Array(3);
i=0
//loop will stop at 4 names
while(i < 4) {
 
var name = prompt("Please enter a name:","");
names[i] = name;
var list = document.createElement('li');
document.getElementById('nameList').appendChild(list).innerHTML = names[i];
i++
}
}
 
  1. function getNames()
  2. {
  3.    // set up the array
  4. var names=new Array(3);
  5. i=0
  6. //loop will stop at 4 names
  7. while(i < 4) {
  8.  
  9. var name = prompt("Please enter a name:","");
  10. names[i] = name;
  11. var list = document.createElement('li');
  12. document.getElementById('nameList').appendChild(list).innerHTML = names[i];
  13. i++
  14. }
  15. }
  16.  
Use your words like arrows to shoot toward your goal.
  • Satwant
  • Graduate
  • Graduate
  • User avatar
  • Joined: Dec 27, 2010
  • Posts: 126
  • Loc: Bangalore
  • Status: Offline

Post January 21st, 2011, 10:29 pm

So is it just Unterminated Loop ?? :)
  • mindfullsilence
  • Professor
  • Professor
  • User avatar
  • Joined: Aug 04, 2008
  • Posts: 846
  • Status: Offline

Post January 24th, 2011, 11:39 pm

No, it terminates with a closing bracket; the problem was in line 8 where I was using the wrong syntax for the "createElement" method.
Use your words like arrows to shoot toward your goal.
  • joebert
  • Sledgehammer
  • Genius
  • No Avatar
  • Joined: Feb 10, 2004
  • Posts: 13455
  • Loc: Florida
  • Status: Offline

Post January 25th, 2011, 5:51 pm

Something about chaining appendChild like that makes me cringe. I don't know why, but changing scope within a chain of command like that just seems wrong to me. convenient, perhaps, complicated and a nightmare to debug, likely. :)
Strong with this one, the sudo is.
  • mindfullsilence
  • Professor
  • Professor
  • User avatar
  • Joined: Aug 04, 2008
  • Posts: 846
  • Status: Offline

Post January 25th, 2011, 11:22 pm

Honestly I'm just now taking my javscript course so I'm sure I'll have more questions in the coming weeks, and my js will look horrid. Could you explain why chaining the appendChild method is a bad thing, and what you mean by changing the scope?
Use your words like arrows to shoot toward your goal.
  • joebert
  • Sledgehammer
  • Genius
  • No Avatar
  • Joined: Feb 10, 2004
  • Posts: 13455
  • Loc: Florida
  • Status: Offline

Post January 26th, 2011, 7:38 pm

"getElementById" gives you a hint at what the next element referenced in the chain will be, but appendChild is ambiguous. Should a reference to the element a node was appended to be returned, or a reference to the node that was appended ?
Strong with this one, the sudo is.
  • mindfullsilence
  • Professor
  • Professor
  • User avatar
  • Joined: Aug 04, 2008
  • Posts: 846
  • Status: Offline

Post January 26th, 2011, 7:53 pm

I think I understand your point. Would you mind sharing a better solution for the above code for future reference?
Use your words like arrows to shoot toward your goal.
  • joebert
  • Sledgehammer
  • Genius
  • No Avatar
  • Joined: Feb 10, 2004
  • Posts: 13455
  • Loc: Florida
  • Status: Offline

Post January 26th, 2011, 9:50 pm

I would do something like this.

JAVASCRIPT Code: [ Select ]
function getNames()
{
   var names = [];
   while(names.length != 4)
   {
      names = prompt('Enter 4 names, separated by commas:').split(/\s*,\s*/);
   }
 
   document.getElementById('nameList').innerHTML = document.getElementById('nameList').innerHTML
      + '<li>'
      + names.join('</li><li>')
      + '</li>';
}
  1. function getNames()
  2. {
  3.    var names = [];
  4.    while(names.length != 4)
  5.    {
  6.       names = prompt('Enter 4 names, separated by commas:').split(/\s*,\s*/);
  7.    }
  8.  
  9.    document.getElementById('nameList').innerHTML = document.getElementById('nameList').innerHTML
  10.       + '<li>'
  11.       + names.join('</li><li>')
  12.       + '</li>';
  13. }
Strong with this one, the sudo is.
  • mindfullsilence
  • Professor
  • Professor
  • User avatar
  • Joined: Aug 04, 2008
  • Posts: 846
  • Status: Offline

Post January 26th, 2011, 10:04 pm

Just about all of that I understand...except for one piece:

JAVASCRIPT Code: [ Select ]
.split(/\s*,\s*/)
 
  1. .split(/\s*,\s*/)
  2.  


I assume the split is for dimmensioning an array using the commas as the divider for each term. Whether that's correct or not...what is all the "/\s*,\s*/" mumbo jumbo?
Use your words like arrows to shoot toward your goal.
  • joebert
  • Sledgehammer
  • Genius
  • No Avatar
  • Joined: Feb 10, 2004
  • Posts: 13455
  • Loc: Florida
  • Status: Offline

Post January 26th, 2011, 10:11 pm

split is a string method which will accept either a string, or a regular expression as a delimiter.

In this case, it's splitting the string returned from prompt() using a comma, optionally including any amount of whitespace on either side of the comma to account for people who naturally put spaces after or before commas.
Strong with this one, the sudo is.
  • mindfullsilence
  • Professor
  • Professor
  • User avatar
  • Joined: Aug 04, 2008
  • Posts: 846
  • Status: Offline

Post January 26th, 2011, 10:24 pm

okay, so this is what I'm gathering, not knowing anything about regular expressions:
"\s" is the regular expression for space
"*" means "any amount of"

That about right?

Also; the "/" is used for escaping, why are you putting it in different positions for each "\s*"

Eh, it's such a rarity that I sound like such a noob, I promise; but these are things I know I won't learn in class...my college instructor isn't very explanatory.
Use your words like arrows to shoot toward your goal.
  • joebert
  • Sledgehammer
  • Genius
  • No Avatar
  • Joined: Feb 10, 2004
  • Posts: 13455
  • Loc: Florida
  • Status: Offline

Post January 26th, 2011, 11:23 pm

\s is what's called a "meta character". Basically it stands for a group of characters. There are more meta characters, such as \d which means any numeric character, 0-9. \s means "whitespace", not just the space character, but tabs, newlines, etc. I tend to use \s instead of a literal space in my regular expressions because they're easier to read.

Yes, the asterisk "*" in this case means any amount of, or more commonly "zero or more times". It's what's called a quantifier. Other quantifiers include the plus sign which means "one or more times", the question mark which means "zero or one time", and a range which is specified in braces, "{2,7}" which in that example would mean "two to seven times".

The "/" (forward slash) isn't used for escaping, the "\" (backwards slash) is. The backwards slash is also used for marking meta-characters. The forward slashes you see are "pattern delimiters". Pattern delimiters mark the beginning and the end of the pattern.
Strong with this one, the sudo is.
  • mindfullsilence
  • Professor
  • Professor
  • User avatar
  • Joined: Aug 04, 2008
  • Posts: 846
  • Status: Offline

Post January 27th, 2011, 10:02 am

Thanks for taking the time to clear that up JB. I think I've got my fill of new information for the day, but I'm sure there'll be more questions to come later down the road.
Use your words like arrows to shoot toward your goal.

Post Information

  • Total Posts in this topic: 14 posts
  • Users browsing this forum: No registered users and 106 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 2011 Unmelted, LLC. Ozzu® is a registered trademark of Unmelted, LLC.