1
\$\begingroup\$

Task to accomplish: Made a function which counts the amount of words in a sentence.

Please don't put attention to the HTML. Is just for testing-purposes.

var sentence = document.querySelector('#sentence');
var exec = document.querySelector('#exec');
var result = document.querySelector('#result');

// ## THE ACTUAL FUNCTION ###########
// ## Rest is just for testing ######

// Reports the count of words which are 
//   contained in assigned string.

// Parameter (sentence): Are string is 
//   expected. The string doesn't have 
//   to be empty. It has to consist of
//   other characters then just blanks.

// Return a number: The count of words
//   in the assigned string.
//   In case of an error -1 is returned.

function getCountOfWords(sentence) {
  if (sentence.length === 0 || 
      sentence.search(/\w+/) === -1) 
    return -1;

  if (sentence.search(/\s+/) === -1)
    return 1;

  return (sentence.match(/(^|\s)\w+/g)).length;
}

// ## END ACTUAL FUNCTION ###########

function execHandler() {
  result.value = getCountOfWords(sentence.value);
}

exec.addEventListener('click', execHandler);	
.wrap {
  margin: 20px auto;
  width: 900px;
}

.wrap label {
  display: inline-block;
  width: 150px;
}

.wrap div {
  margin: 20px 0;
}
<div class="wrap">
  <div>
    <label for="sentence">
      Sentence to check:
    </label>
    <input type="text" id="sentence" 
           value="one Two2 Three 4four five123"
           size="80" />
  </div>
  <div>
    <button id="exec">Exec</button>
  </div>
  <div>
    <label for="sentence">
      Count of words:
    </label>
    <input type="text" id="result" />
  </div>
</div>

I have tried to catch everything which could wrong. But perhaps there are cases which I haven't thought about?

Are their better (more elegant) ways to achieve the described task?

\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

This seems a bit like reinventing the wheel. I'm not sure on what you're trying to achieve here, but it doesn't look like you're accounting for hyphen separated words, so data-type counts as one word.

It looks like to me that this is just splitting on the space, and it could be replaced by String.split(' ').length

I'm sorry if I missed the point here. But I'm gonna need you to explain it better why you need such a function in the first place.

var sentence = document.querySelector('#sentence');
var exec = document.querySelector('#exec');
var result = document.querySelector('#result');

function execHandler() {
  result.value = sentence.value.split(' ').length;
}

exec.addEventListener('click', execHandler);	
.wrap {
  margin: 20px auto;
  width: 900px;
}

.wrap label {
  display: inline-block;
  width: 150px;
}

.wrap div {
  margin: 20px 0;
}
<div class="wrap">
  <div>
    <label for="sentence">
      Sentence to check:
    </label>
    <input type="text" id="sentence" 
           value="one Two2 Three 4four five123"
           size="80" />
  </div>
  <div>
    <button id="exec">Exec</button>
  </div>
  <div>
    <label for="sentence">
      Count of words:
    </label>
    <input type="text" id="result" />
  </div>
</div>

\$\endgroup\$
2
  • \$\begingroup\$ First: Thanks for your answer. It shall become part of a application which reports the percentage-rate of how often a word is used in a provided text. Example: You got a text with 250 words and the word "blue" appears in it 5 times == 2%. \$\endgroup\$
    – ts248
    Mar 29, 2016 at 15:28
  • \$\begingroup\$ @ts248 In that case, you can calculate that with this ES6 function. \$\endgroup\$
    – Scott
    Mar 30, 2016 at 5:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.