If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Enjoy an ad free experience by logging in. Not a member yet? Register.

I don t see anyone of your method Pedant!!! xD
And your last comment!! It's a mystery for me \s+\... I don't think it's a code for beginner
However thanks for all example, I'll remember for the next time!!!!

function start ()
{
var arrayPhrase = "Yesterday is history. Tomorrow is a mystery. But today is a gift. For this it is called present.";
// split breaks a sting into an ARRAY of strings, all broken
// apart on the character (or regular expression) you give:
var phrases = arrayPhrase.split(".");
// so now we have each sentence (that used to end in a period)
// in a separate element of the array named phrases
var wordsPerPhrase = [ ];
var totalWords = 0;
// loop through all the phrases/sentences in the phrases array
// notice that we do *NOT* process the last element of the array
// because it will be the blank string after the last period
for ( var p = 0; p < phrases.length - 1; ++p )
{
// this is the trickiest part, so let's break it down in pieces:
// phrases[p] :: get one phrase from the array
// .replace(/^\s+/,"") :: replace all *leading* spaces with nothing
// .replace(/\s+$/,"") :: replace all *trailing* spaces with nothing
// (in other words, trim the spaces off both ends
// .split(" "); :: split the phrase into words, just as we split into phrases
var words = phrases[p].replace(/^\s+/,"").replace(/\s+$/,"").split(" ");
// so now we have an array named words...and it has as many elements
// as there are words in the phrase!
// put the count of words in to proper element of the wordsPerPhrase array
wordsPerPhrase[p] = words.length;
// and bump the count of the total number of words
totalWords += words.length;
}
// and then just show the final numbers:
// I corrected the value for Total Phrases from my prior post
alert ( "Total words = " + totalWords + "\n\n"
+ "Total phrases = " + (phrases.length-1) + "\n\n"
+ "Words in each phrase = " + wordsPerPhrase.join(", ")
);
}

So what does your code look like now. Also are there any error messages on the error console?

No error in console, but I find error!!!
When I write array = ["Yehxcwj ecc...."]
the programs read this how to one element of array, so when start cicle for (i = 0; i < array.length; i++), the programs execute only one for because the length of array is 0. How to fix this problem!?!?! Beacause my problem ask: " Is stored in an array a text of char. Print the numbers of word, phrase and word for phrase!!!".
If I use an simple var the progrma work perfectly