Jump to content

I can't find the problem in this simple Code


redeyedfrog

Recommended Posts

Hello everyoneI tried too much to find whats wrong in this very simple code but I cant find the bug.any help will be appreciatedanother question is that is there any program to help me find the bugs?if yes whats that?

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head><title>Time</title><script language="javascript">var names=new Arry();var i=0;while(next!="") {var next=prompt("Enter your name here");if(next!="")names[i]=next;i=i+1;}document.write("Hey we have " + names.length + " here!")</script></head><body><script language="javascript">document.write("We have " + names.length + " here!")</script></body>

Link to comment
Share on other sites

Hello everyoneI tried too much to find whats wrong in this very simple code but I cant find the bug.any help will be appreciatedanother question is that is there any program to help me find the bugs?if yes whats that?
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head><title>Time</title><script language="javascript">var names=new Arry();var i=0;while(next!="") {var next=prompt("Enter your name here");if(next!="")names[i]=next;i=i+1;}document.write("Hey we have " + names.length + " here!")</script></head><body><script language="javascript">document.write("We have " + names.length + " here!")</script></body>

Um..First define next before the loop.Secondly array is miss spelled.Third do you know what you are using next both in the while condition and the if statement which means you would only get one name each time you load the page.FireBug in FirefoxChrome has one built in found in the java console if I remember correctly.
Link to comment
Share on other sites

Uh...Thanks for answering my friend but I didn't get the first and the third :)would you please correct the code if its possible? (I'm just so much newbie in java script)

Link to comment
Share on other sites

he's saying you have not declared next as variable for javascript. secondly, you are using it twice, so you are either overwriting it or using for different things, which may be causing you to experience unpredictable results. Some of these errors could have been discovered by using the error console for what every browsers you are using.

Link to comment
Share on other sites

I changed it to this:there is still a pproblemfirebug tell me the problem is in:var names=new Array()I'm totally disappointed each time I write code there is a problem :)

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head><title>Time</title><script language="javascript">var names=new Array()names[i]=next;var i=0;var next=prompt("Enter your name here");while(next!="") {i=i+1;}document.write("Hery we have " + names.length + " here!")</script></head><body><script language="javascript">document.write("We have " + names.length + " here!")</script></body>

Link to comment
Share on other sites

More likely the first error is here:names=next;i is not yet defined. next is not yet defined. When you fix that, you will have to fix this:var next=prompt("Enter your name here");while(next!="") {nowhere in the loop does next get reset. It will always equal the value returned by the prompt, so the loop will run forever.And what is going on inside the loop? i gets bigger and bigger, but you don't use it for anything.

Link to comment
Share on other sites

Got it! haha!I changed it to this and it works correctly! whoooa! all I've done was bringing all of the variables into the loop and it workedcan someone explain me what exactly happened when the variables was outside the loop?I mean why it didn't work when the where out of the while loop? (there is a before and after!)Thank you so much my friends you are the bestThe Before Code(wrong one):

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN""http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"><head><title>Time</title><script language="javascript">var names=new Array()names[i]=next;var i=0;var next=prompt("Enter your name here");while(next!="") {i=i+1;}document.write("Hey we have " + names.length + " new friends here!can you believe it?!")</script></head><body></body>

The After Code(corrected one):

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head><title>Time</title><script language="javascript">var i=0;while(next !="") {var next=prompt("Enter your name here");var names=new Array()names[i]=next;i++;}document.write("Hey! we have " + names.length + " new friends here!can you believe it?!")</script></head><body></body>

Link to comment
Share on other sites

That's not right either.You're re-declaring the variables on each iteration of the loop. And you're deleting the previous array every time so that the resulting array only has one element.Leave the "var" statements and the "new Array()" statement out of the loop:

var next = "non-empty string";var names = new Array();var i = 0;while(next !="") {  next = prompt("Enter your name here");  names[i] = next;  i++;}

Link to comment
Share on other sites

That's not right either.You're re-declaring the variables on each iteration of the loop. And you're deleting the previous array every time so that the resulting array only has one element.Leave the "var" statements and the "new Array()" statement out of the loop:
Thanks for answering my friendsorry I'm just so much newbie in javascriptwould you please explain more an a little simpler? :)
Link to comment
Share on other sites

have you tried his example? That's the explanation. Before names was always equal to a new array, as opposed to an array you could keep adding elements to. So it's length would alway be 1, and you were never storing the previous submissions.

Link to comment
Share on other sites

You declare two variables inside your while loop using the var keyword. This is incorrect. Some browsers will not mind, but some will create a new variable every time the loop comes around (that is what var should do) and others may simply throw an error.Declare each variable with the var keyword BEFORE the loop, so that you do it only once.

Link to comment
Share on other sites

Having the array in the loop would mean that every time you enter the loop the array's data is deleted.This is what the array would look like if you have "names = new Array()" inside the loop:

i = 0: [ "Name1" ]i = 1: [ , "Name2" ]i = 2: [ , , "Name3" ]i = 3: [ , , , "Name4" ]i = 4: [ , , , , "Name5" ]

Link to comment
Share on other sites

Got itThanks my friends

Link to comment
Share on other sites

but there is still a problem leftWhat does var next = "non-empty string"; that Ingolme said do?when I delete this line the script doesn't work I think this line is necessary to have a stable "next" variable because the "next" variable inside the loop is not stablebut if the reason is what I said and if the variables inside the loop include "next" variable (the one inside the loop) are deleted each time , why when I add document.write(names); at the end of the script(out of the loop) it shows the contents saved in the "next" variable inside the loop?(excuse me these are my first steps of programing and I'm a little confused)

Link to comment
Share on other sites

the next variable needs to be defined, that means that it needs a value.Normally, I initialize a string with an empty string value ( var next = ""; ) but then it doesn't meet the condition of your while() loop (next != "") so nothing will happen.To initialize next you can put anything you like as long as it's not an empty string. You could write var next = "Hello World!"; if you wanted to, it's just necessary to assign a value to it.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...