PDA

View Full Version : split(), .length - counting total words



susie123
09-29-2007, 04:31 PM
Hello

I have written this code and everything works fine except that instead of receiving a total number of WORDS, Im receiving the total number of characters. Can anybody please advise where Im going wrong, please?

<script language="JavaScript">
<!--
var wholeName = prompt("Please enter your name & surname.","");
var first_split = wholeName.split("&nbsp;");
var resource = first_split[1];
var totalWords = first_split[0];
document.write(totalWords);
alert("Total number of words = " + totalWords.length)
//-->
</script>

Many thanks as always. S

Twey
09-29-2007, 05:44 PM
<script type="text/javascript">
alert("Total number of words: " + prompt("Please enter your name and surname:").split(" ").length);
</script>

susie123
09-30-2007, 10:01 AM
Thank you very much Twey :)