Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 2 of 2 FirstFirst 12
Results 16 to 22 of 22
  1. #16
    New Coder
    Join Date
    Oct 2012
    Location
    Italy
    Posts
    72
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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

  2. #17
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,602
    Thanks
    78
    Thanked 4,387 Times in 4,352 Posts
    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.

  3. #18
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    Google translate uses vector:

    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

  4. #19
    Regular Coder
    Join Date
    May 2012
    Location
    France
    Posts
    224
    Thanks
    0
    Thanked 32 Times in 30 Posts
    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.

  5. #20
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,602
    Thanks
    78
    Thanked 4,387 Times in 4,352 Posts
    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:
    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.

  6. #21
    Regular Coder
    Join Date
    May 2012
    Location
    France
    Posts
    224
    Thanks
    0
    Thanked 32 Times in 30 Posts
    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 !

  7. #22
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,602
    Thanks
    78
    Thanked 4,387 Times in 4,352 Posts
    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.


 
Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •