Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-05-2013, 04:10 PM   PM User | #16
triko
New Coder

 
Join Date: Oct 2012
Location: Italy
Posts: 72
Thanks: 3
Thanked 0 Times in 0 Posts
triko is an unknown quantity at this point
Quote:
Originally Posted by Old Pedant View Post
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
triko is offline   Reply With Quote
Old 01-05-2013, 11:43 PM   PM User | #17
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,210
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Just badly written, I think.

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.
Old Pedant is offline   Reply With Quote
Old 01-05-2013, 11:59 PM   PM User | #18
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
Google translate uses vector:

Quote:
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
AndrewGSW is offline   Reply With Quote
Old 01-07-2013, 02:01 AM   PM User | #19
007julien
Regular Coder

 
Join Date: May 2012
Location: France
Posts: 115
Thanks: 0
Thanked 17 Times in 15 Posts
007julien is an unknown quantity at this point
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..
007julien is offline   Reply With Quote
Old 01-08-2013, 01:15 AM   PM User | #20
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,210
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Quote:
Originally Posted by 007julien View Post
This solution could be improved by using word boundaries (\b) and all punctuation marks...
Yes, but it still won't be right in any absolute sense.

For example, try this text:
Quote:
Dr. I. M. Nuzzo, also known as "Psycho Man," was present at the A.A. meeting.
__________________
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.
Old Pedant is offline   Reply With Quote
Old 01-08-2013, 09:44 AM   PM User | #21
007julien
Regular Coder

 
Join Date: May 2012
Location: France
Posts: 115
Thanks: 0
Thanked 17 Times in 15 Posts
007julien is an unknown quantity at this point
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 !
007julien is offline   Reply With Quote
Old 01-08-2013, 06:26 PM   PM User | #22
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,210
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:21 AM.


Advertisement
Log in to turn off these ads.