Click to See Complete Forum and Search --> : Why does it always start out with undefined


pwned
08-03-2003, 09:46 PM
i have this script written to "pig latinize" a string

<html>
<head>
<title>
E.C.
</title>
<font color = "black">
<script language="javascript">

pigPrompt = prompt("Type in sentence you want to be 'pig latinized'");


var splitArray = pigPrompt.split(" ");




for (var i = 0; i < splitArray.length; i++)
{
var words = splitArray[i];

slicer = words.slice(0, 1);

sstring = words.substring(1, words.length)


var together = sstring + slicer +"ay ";

var together2 = together2 += together;


}

document.writeln(together2);


</script>
</html>



now, everything works ok...except it sticks an "undefined" at the beginning

I dont know where this could be coming from


for example if you wrote in "hello how are you today"
it would result in
"undefinedelloHay owhay reaay ouyay odaytay"

If anyone could help me i'd be greatful, thank you.
Eric

pyro
08-03-2003, 10:00 PM
Something like this might work better for you:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Pig Latin</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<script type="text/javascript">

function pigLatin() {
pigPrompt = prompt("Type in sentence you want to be 'pig latinized'","");
var splitArray = pigPrompt.split(" ");
var together2 = "";
for (var i = 0; i < splitArray.length; i++) {
var words = splitArray[i];
slicer = words.slice(0, 1);
sstring = words.substring(1, words.length)
var together = sstring + slicer +"ay ";
together2 += together;
}
document.getElementById("piglatin").innerHTML = together2;
}
window.onload = pigLatin;
</script>

</head>
<body>
<div id="piglatin"></div>
</body>
</html>The main benefit of that method (besides fixing the error ;) ) is that users can hit refresh and they will get the promp again (as the script doesn't use document.write or document.writeln).

BTW, the error was in this line:

var together2 = together2 += together;

You should have defined the variable earlier in the script, and then used together2 += together, like I did in my script.

pwned
08-03-2003, 10:09 PM
Thanks a lot for the quick answer and easy fix.
Also another question...Do you know of a website that has a function list for JavaScript...similar to the one that comes packaged with Flash, or the one on the PHP site for php...Ive found it exceedingly hard to find one for javascript.

pyro
08-03-2003, 10:13 PM
For online use, use the javascript 1.3 guide: http://devedge.netscape.com/library/manuals/2000/javascript/1.3/guide/
For offline, I must recommend "JavaScript, The Definitive Guide" by David Flanagan...

pwned
08-03-2003, 10:16 PM
Originally posted by pyro
For online use, use the javascript 1.3 guide: http://devedge.netscape.com/library/manuals/2000/javascript/1.3/guide/
For offline, I must recommend "JavaScript, The Definitive Guide" by David Flanagan...



Alright, again thanks a lot

pyro
08-03-2003, 10:19 PM
You're welcome... :)