Need a quick debug, please

  • mindfullsilence
  • Professor
  • Professor
  • User avatar
  • Posts: 854

Post 3+ Months Ago

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.
  • mindfullsilence
  • Professor
  • Professor
  • User avatar
  • Posts: 854

Post 3+ Months Ago

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.  
  • Satwant
  • Graduate
  • Graduate
  • User avatar
  • Posts: 127
  • Loc: Bangalore

Post 3+ Months Ago

So is it just Unterminated Loop ?? :)
  • mindfullsilence
  • Professor
  • Professor
  • User avatar
  • Posts: 854

Post 3+ Months Ago

No, it terminates with a closing bracket; the problem was in line 8 where I was using the wrong syntax for the "createElement" method.
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13504
  • Loc: Florida

Post 3+ Months Ago

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. :)
  • mindfullsilence
  • Professor
  • Professor
  • User avatar
  • Posts: 854

Post 3+ Months Ago

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?
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13504
  • Loc: Florida

Post 3+ Months Ago

"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 ?
  • mindfullsilence
  • Professor
  • Professor
  • User avatar
  • Posts: 854

Post 3+ Months Ago

I think I understand your point. Would you mind sharing a better solution for the above code for future reference?
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13504
  • Loc: Florida

Post 3+ Months Ago

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. }
  • mindfullsilence
  • Professor
  • Professor
  • User avatar
  • Posts: 854

Post 3+ Months Ago

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?
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13504
  • Loc: Florida

Post 3+ Months Ago

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.
  • mindfullsilence
  • Professor
  • Professor
  • User avatar
  • Posts: 854

Post 3+ Months Ago

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.
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13504
  • Loc: Florida

Post 3+ Months Ago

\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.
  • mindfullsilence
  • Professor
  • Professor
  • User avatar
  • Posts: 854

Post 3+ Months Ago

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.

Post Information

  • Total Posts in this topic: 14 posts
  • Users browsing this forum: No registered users and 55 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
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.