How to calculate number of words with given price?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sarfraznawaz
    New Member
    • Aug 2010
    • 3

    How to calculate number of words with given price?

    Hi,

    I am Web Designer.

    I am new to this coding related job. Can anyone help me how to sort this out.

    How to calculate number of words with given price?

    Thanks.
    Sarfraz
    Attached Files
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5390

    #2
    it is a bit unreliable to calculate the price clientside since that could easyly be modified by the user - so to have a solid calculation you would need a serverside calculation too. to give the user a price-preview you might do it with javascript ... a simple approach would be to retrieve the text from the textarea and then the number of the words is roughly:

    Code:
    var textareaNode = document.getElementById('textareaId');
    var noOfWords = textareaNode.value.split(' ').length;
    this would give you the 'wordcount' depending on whitespace-separated 'character-groups' ... in case you would need more specific rules to count the words like 'she's' should be two words? then you would need a bit more preparing processing of the text.

    Comment

    • sarfraznawaz
      New Member
      • Aug 2010
      • 3

      #3
      Hi Bro

      Thanks for the info will work on the concept. Thanks very much

      Comment

      • kovik
        Recognized Expert Top Contributor
        • Jun 2007
        • 1044

        #4
        That solution is too basic. What about surrounding whitespace and double-spaces? Sometimes that double-space might even start looking like a triple-space! :p


        You should trim the whitespace off of the edges and condense the internal whitespace, THEN split the string. I'd make it into a function. Hell, I'd add it to the String object. In fact, I shall. :3


        Functions:
        Code:
        String.prototype.trim = function() {
          return this.replace(/^\s+/,'').replace(/\s+$/,'');
        }
        
        String.prototype.condense = function() {
          return this.replace(/\s+/,' ');
        }
        
        String.prototype.words = function() {
          return (this.trim().condense().split(' ')).length;
        }

        Test:
        Code:
        alert(("  this is   a test    string    ").words());

        Result:
        Code:
        5
        :)

        Comment

        • gits
          Recognized Expert Moderator Expert
          • May 2007
          • 5390

          #5
          of course - i just wanted to show a start - it could even be extended to find out divided words etc.

          Comment

          • kovik
            Recognized Expert Top Contributor
            • Jun 2007
            • 1044

            #6
            No doubt. :)

            Comment

            • sarfraznawaz
              New Member
              • Aug 2010
              • 3

              #7
              Hi Bro

              Nope thanks bro

              Comment

              Working...