Felgall already answered that question:
So just do
Code:
var text = arrayPhrase[0];
and then replace all uses of arrayPhrase after that point with text.
But I strongly suspect you are misreading the homework assignment. Or the instructor just used the wrong words. Maybe you should copy the assignment here *VERBATIM*.
Ok but it is in italian the text!!
In un vettore viene memorizzato un testo di caratteri. Ogni frase è chiusa da un punto e le singole parole sono divise da spazi. Conta il numero di frasi presenti nel testo e, per ognuna, fornisci anche il numero di parole di cui è composta.
My translate in English
In an array is stored an text of character. Each sentence is close by full stop and the each word are separated by space. Count the numbers of sentences on the text and, for each, provide the number of words it contains
I do *NOT* believe the instructor *really* means an array.
It's just that he was probably thinking of the string *AS IF* it is an array of characters. And, indeed, in most modern browsers you can use a string as an array of characters.
That is you can do either someString.charAt(x) or you can do someString[x]
Doesn't work in all browsers, but it does in most.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Vector is stored in a text character. Each sentence is closed by a point and the individual words are separated by spaces. Count the number of sentences in the text and, for each, provide the number of words it contains.
which is still ambiguous.
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS
A variant for well-constructed sentences with points and unique spaces, but without commas and other punctuations ...
Code:
function start (){
var strPhr = "Yesterday is history. Tomorrow is a mystery. But today is a gift. For this it is called present.";
var wrd=0,arrPhr=[];
arrPhr[0]=0;
strPhr.replace(/\. ?| /g,
function(a){wrd++;arrPhr[arrPhr.length-1]++;if (/\. /.test(a)) arrPhr[arrPhr.length]=0;})
alert ( "Total words = " + wrd + "\n\n"
+ "Total phrases = " + arrPhr.length + "\n\n"
+ "Words in each phrase = " + arrPhr.join(", "));
}
start();
This solution could be improved by using word boundaries (\b) and all punctuation marks...
Last edited by 007julien; 01-07-2013 at 02:13 AM..
The question of points following capital letters seems not insoluble...
But effectively, the question is certainly rather subtle. My word processor gives nevertheless 15 words in your sentence without venturing to count the sentences !
Interesting. It' apparently counted "A.A." as one word. I would bet that if I put a space in there--"A. A."--then it would have found 16 words. That's pretty smart of it, actually.
But I do wonder how many sentences a good sentence counter would find in that.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.