Counting words in string

Javascript

Javascript Problem Overview


I was trying to count words in a text in this way:

function WordCount(str) {
  var totalSoFar = 0;
  for (var i = 0; i < WordCount.length; i++)
    if (str(i) === " ") { // if a space is found in str
      totalSoFar = +1; // add 1 to total so far
  }
  totalsoFar += 1; // add 1 to totalsoFar to account for extra space since 1 space = 2 words
}

console.log(WordCount("Random String"));

I think I have got this down pretty well, except I think that the if statement is wrong. The part that checks if str(i) contains a space and adds 1.

Edit:

I found out (thanks to Blender) that I can do this with a lot less code:

function WordCount(str) { 
  return str.split(" ").length;
}

console.log(WordCount("hello world"));

Javascript Solutions


Solution 1 - Javascript

Use square brackets, not parentheses:

str[i] === " "

Or charAt:

str.charAt(i) === " "

You could also do it with .split():

return str.split(' ').length;

Solution 2 - Javascript

Try these before reinventing the wheels

from https://stackoverflow.com/questions/6543917/count-number-of-word-from-given-string-using-javascript

function countWords(str) {
  return str.trim().split(/\s+/).length;
}

from http://www.mediacollege.com/internet/javascript/text/count-words.html

function countWords(s){
	s = s.replace(/(^\s*)|(\s*$)/gi,"");//exclude  start and end white-space
	s = s.replace(/[ ]{2,}/gi," ");//2 or more space to 1
	s = s.replace(/\n /,"\n"); // exclude newline with a start spacing
	return s.split(' ').filter(function(str){return str!="";}).length;
    //return s.split(' ').filter(String).length; - this can also be used
}

from Use JavaScript to count words in a string, WITHOUT using a regex - this will be the best approach

function WordCount(str) {
     return str.split(' ')
            .filter(function(n) { return n != '' })
            .length;
}

> Notes From Author: > >You can adapt this script to count words in whichever way you like. The important part is s.split(' ').length — this counts the > spaces. > The script attempts to remove all extra spaces (double spaces etc) before counting. > If the text contains two words without a space between them, it will count them as one word, e.g. "First sentence >.Start of next sentence".

Solution 3 - Javascript

One more way to count words in a string. This code counts words that contain only alphanumeric characters and "_", "’", "-", "'" chars.

function countWords(str) {
  var matches = str.match(/[\w\d\’\'-]+/gi);
  return matches ? matches.length : 0;
}

Solution 4 - Javascript

After cleaning the string, you can match non-whitespace characters or word-boundaries.

Here are two simple regular expressions to capture words in a string:

  • Sequence of non-white-space characters: /\S+/g
  • Valid characters between word boundaries: /\b[a-z\d]+\b/g

The example below shows how to retrieve the word count from a string, by using these capturing patterns.

/*Redirect console output to HTML.*/document.body.innerHTML='';console.log=function(s){document.body.innerHTML+=s+'\n';};
/*String format.*/String.format||(String.format=function(f){return function(a){return f.replace(/{(\d+)}/g,function(m,n){return"undefined"!=typeof a[n]?a[n]:m})}([].slice.call(arguments,1))});

// ^ IGNORE CODE ABOVE ^
//   =================

// Clean and match sub-strings in a string.
function extractSubstr(str, regexp) {
    return str.replace(/[^\w\s]|_/g, '')
        .replace(/\s+/g, ' ')
        .toLowerCase().match(regexp) || [];
}

// Find words by searching for sequences of non-whitespace characters.
function getWordsByNonWhiteSpace(str) {
    return extractSubstr(str, /\S+/g);
}

// Find words by searching for valid characters between word-boundaries.
function getWordsByWordBoundaries(str) {
    return extractSubstr(str, /\b[a-z\d]+\b/g);
}

// Example of usage.
var edisonQuote = "I have not failed. I've just found 10,000 ways that won't work.";
var words1 = getWordsByNonWhiteSpace(edisonQuote);
var words2 = getWordsByWordBoundaries(edisonQuote);

console.log(String.format('"{0}" - Thomas Edison\n\nWord count via:\n', edisonQuote));
console.log(String.format(' - non-white-space: ({0}) [{1}]', words1.length, words1.join(', ')));
console.log(String.format(' - word-boundaries: ({0}) [{1}]', words2.length, words2.join(', ')));

body { font-family: monospace; white-space: pre; font-size: 11px; }


Finding Unique Words

You could also create a mapping of words to get unique counts.

function cleanString(str) {
    return str.replace(/[^\w\s]|_/g, '')
        .replace(/\s+/g, ' ')
        .toLowerCase();
}

function extractSubstr(str, regexp) {
    return cleanString(str).match(regexp) || [];
}

function getWordsByNonWhiteSpace(str) {
    return extractSubstr(str, /\S+/g);
}

function getWordsByWordBoundaries(str) {
    return extractSubstr(str, /\b[a-z\d]+\b/g);
}

function wordMap(str) {
    return getWordsByWordBoundaries(str).reduce(function(map, word) {
        map[word] = (map[word] || 0) + 1;
        return map;
    }, {});
}

function mapToTuples(map) {
    return Object.keys(map).map(function(key) {
        return [ key, map[key] ];
    });
}

function mapToSortedTuples(map, sortFn, sortOrder) {
    return mapToTuples(map).sort(function(a, b) {
        return sortFn.call(undefined, a, b, sortOrder);
    });
}

function countWords(str) {
    return getWordsByWordBoundaries(str).length;
}

function wordFrequency(str) {
    return mapToSortedTuples(wordMap(str), function(a, b, order) {
        if (b[1] > a[1]) {
            return order[1] * -1;
        } else if (a[1] > b[1]) {
            return order[1] * 1;
        } else {
            return order[0] * (a[0] < b[0] ? -1 : (a[0] > b[0] ? 1 : 0));
        }
    }, [1, -1]);
}

function printTuples(tuples) {
    return tuples.map(function(tuple) {
        return padStr(tuple[0], ' ', 12, 1) + ' -> ' + tuple[1];
    }).join('\n');
}

function padStr(str, ch, width, dir) { 
    return (width <= str.length ? str : padStr(dir < 0 ? ch + str : str + ch, ch, width, dir)).substr(0, width);
}

function toTable(data, headers) {
    return $('<table>').append($('<thead>').append($('<tr>').append(headers.map(function(header) {
        return $('<th>').html(header);
    })))).append($('<tbody>').append(data.map(function(row) {
        return $('<tr>').append(row.map(function(cell) {
            return $('<td>').html(cell);
        }));
    })));
}

function addRowsBefore(table, data) {
    table.find('tbody').prepend(data.map(function(row) {
        return $('<tr>').append(row.map(function(cell) {
            return $('<td>').html(cell);
        }));
    }));
    return table;
}

$(function() {
    $('#countWordsBtn').on('click', function(e) {
        var str = $('#wordsTxtAra').val();
        var wordFreq = wordFrequency(str);
        var wordCount = countWords(str);
        var uniqueWords = wordFreq.length;
        var summaryData = [
            [ 'TOTAL', wordCount ],
            [ 'UNIQUE', uniqueWords ]
        ];
        var table = toTable(wordFreq, ['Word', 'Frequency']);
        addRowsBefore(table, summaryData);
        $('#wordFreq').html(table);
    });
});

table {
    border-collapse: collapse;
    table-layout: fixed;
    width: 200px;
    font-family: monospace;
}
thead {
    border-bottom: #000 3px double;;
}
table, td, th {
    border: #000 1px solid;
}
td, th {
    padding: 2px;
    width: 100px;
    overflow: hidden;
}

textarea, input[type="button"], table {
    margin: 4px;
    padding: 2px;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<h1>Word Frequency</h1>
<textarea id="wordsTxtAra" cols="60" rows="8">Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal.

Now we are engaged in a great civil war, testing whether that nation, or any nation so conceived and so dedicated, can long endure. We are met on a great battle-field of that war. We have come to dedicate a portion of that field, as a final resting place for those who here gave their lives that that nation might live. It is altogether fitting and proper that we should do this.

But, in a larger sense, we can not dedicate -- we can not consecrate -- we can not hallow -- this ground. The brave men, living and dead, who struggled here, have consecrated it, far above our poor power to add or detract. The world will little note, nor long remember what we say here, but it can never forget what they did here. It is for us the living, rather, to be dedicated here to the unfinished work which they who fought here have thus far so nobly advanced. It is rather for us to be here dedicated to the great task remaining before us -- that from these honored dead we take increased devotion to that cause for which they gave the last full measure of devotion -- that we here highly resolve that these dead shall not have died in vain -- that this nation, under God, shall have a new birth of freedom -- and that government of the people, by the people, for the people, shall not perish from the earth.</textarea><br />
<input type="button" id="countWordsBtn" value="Count Words" />
<div id="wordFreq"></div>

Solution 5 - Javascript

I think this method is more than you want

var getWordCount = function(v){
	var matches = v.match(/\S+/g) ;
	return matches?matches.length:0;
}

Solution 6 - Javascript

String.prototype.match returns an array, we can then check the length,

I find this method to be most descriptive

var str = 'one two three four five';

str.match(/\w+/g).length;

Solution 7 - Javascript

The easiest way I've find so far is to use a regex with split.

var calculate = function() {
  var string = document.getElementById('input').value;
  var length = string.split(/[^\s]+/).length - 1;
  document.getElementById('count').innerHTML = length;
};

<textarea id="input">My super text that does 7 words.</textarea>
<button onclick="calculate()">Calculate</button>
<span id="count">7</span> words

Solution 8 - Javascript

This will handle all of the cases and is as efficient as possible. (You don't want split(' ') unless you know beforehand that there are no spaces of greater length than one.):

var quote = `Of all the talents bestowed upon men, 
              none is so precious as the gift of oratory. 
              He who enjoys it wields a power more durable than that of a great king. 
              He is an independent force in the world. 
              Abandoned by his party, betrayed by his friends, stripped of his offices, 
              whoever can command this power is still formidable.`;

function wordCount(text = '') {
  return text.split(/\S+/).length - 1;
};

console.log(wordCount(quote));//59
console.log(wordCount('f'));//1
console.log(wordCount('  f '));//1
console.log(wordCount('   '));//0

Solution 9 - Javascript

The answer given by @7-isnotbad is extremely close, but doesn't count single-word lines. Here's the fix, which seems to account for every possible combination of words, spaces and newlines.

function countWords(s){
	s = s.replace(/\n/g,' '); // newlines to space
	s = s.replace(/(^\s*)|(\s*$)/gi,''); // remove spaces from start + end
	s = s.replace(/[ ]{2,}/gi,' '); // 2 or more spaces to 1
	return s.split(' ').length; 
}

Solution 10 - Javascript

function countWords(str) {
    var regEx = /([^\u0000-\u007F]|\w)+/g;  
    return str.match(regEx).length;
}

Explanation:

/([^\u0000-\u007F]|\w) matches word characters - which is great -> regex does the heavy lifting for us. (This pattern is based on the following SO answer: https://stackoverflow.com/a/35743562/1806956 by @Landeeyo)

+ matches the whole string of the previously specified word characters - so we basically group word characters.

/g means it keeps looking till the end.

str.match(regEx) returns an array of the found words - so we count its length.

Solution 11 - Javascript

For those who want to use Lodash can use the _.words function:

var str = "Random String";
var wordCount = _.size(_.words(str));
console.log(wordCount);

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>

Solution 12 - Javascript

Here's my approach, which simply splits a string by spaces, then for loops the array and increases the count if the array[i] matches a given regex pattern.

    function wordCount(str) {
        var stringArray = str.split(' ');
        var count = 0;
        for (var i = 0; i < stringArray.length; i++) {
            var word = stringArray[i];
            if (/[A-Za-z]/.test(word)) {
                count++
            }
        }
        return count
    }

Invoked like so:

var str = "testing strings here's a string --..  ? // ... random characters ,,, end of string";
wordCount(str)

(added extra characters & spaces to show accuracy of function)

The str above returns 10, which is correct!

Solution 13 - Javascript

There may be a more efficient way to do this, but this is what has worked for me.

function countWords(passedString){
  passedString = passedString.replace(/(^\s*)|(\s*$)/gi, '');
  passedString = passedString.replace(/\s\s+/g, ' '); 
  passedString = passedString.replace(/,/g, ' ');  
  passedString = passedString.replace(/;/g, ' ');
  passedString = passedString.replace(/\//g, ' ');  
  passedString = passedString.replace(/\\/g, ' ');  
  passedString = passedString.replace(/{/g, ' ');
  passedString = passedString.replace(/}/g, ' ');
  passedString = passedString.replace(/\n/g, ' ');  
  passedString = passedString.replace(/\./g, ' '); 
  passedString = passedString.replace(/[\{\}]/g, ' ');
  passedString = passedString.replace(/[\(\)]/g, ' ');
  passedString = passedString.replace(/[[\]]/g, ' ');
  passedString = passedString.replace(/[ ]{2,}/gi, ' ');
  var countWordsBySpaces = passedString.split(' ').length; 
  return countWordsBySpaces;

}

its able to recognise all of the following as separate words:

abc,abc = 2 words,
abc/abc/abc = 3 words (works with forward and backward slashes),
abc.abc = 2 words,
abc[abc]abc = 3 words,
abc;abc = 2 words,

(some other suggestions I've tried count each example above as only 1 x word) it also:

  • ignores all leading and trailing white spaces

  • counts a single-letter followed by a new line, as a word - which I've found some of the suggestions given on this page don't count, for example:
    a
    a
    a
    a
    a
    sometimes gets counted as 0 x words, and other functions only count it as 1 x word, instead of 5 x words)

if anyone has any ideas on how to improve it, or cleaner / more efficient - then please add you 2 cents! Hope This Helps Someone out.

Solution 14 - Javascript

let leng = yourString.split(' ').filter(a => a.trim().length > 0).length

Solution 15 - Javascript

Accuracy is also important.

What option 3 does is basically replace all the but any whitespaces with a +1 and then evaluates this to count up the 1's giving you the word count.

It's the most accurate and fastest method of the four that I've done here.

Please note it is slower than return str.split(" ").length; but it's accurate when compared to Microsoft Word.

See file ops/s and returned word count below.

Here's a link to run this bench test. https://jsbench.me/ztk2t3q3w5/1

// This is the fastest at 111,037 ops/s ±2.86% fastest
var str = "All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy.";
function WordCount(str) {
  return str.split(" ").length;
}
console.log(WordCount(str));
// Returns 241 words. Not the same as Microsoft Word count, of by one.

// This is the 2nd fastest at 46,835 ops/s ±1.76% 57.82% slower
var str = "All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy.";
function WordCount(str) {
  return str.split(/(?!\W)\S+/).length;
}
console.log(WordCount(str));
// Returns 241 words. Not the same as Microsoft Word count, of by one.

// This is the 3rd fastest at 37,121 ops/s ±1.18% 66.57% slower
var str = "All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy.";
function countWords(str) {
  var str = str.replace(/\S+/g,"\+1");
  return eval(str);
}
console.log(countWords(str));
// Returns 240 words. Same as Microsoft Word count.

// This is the slowest at 89 ops/s 17,270 ops/s ±2.29% 84.45% slower
var str = "All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy.";
function countWords(str) {
  var str = str.replace(/(?!\W)\S+/g,"1").replace(/\s*/g,"");
  return str.lastIndexOf("");
}
console.log(countWords(str));
// Returns 240 words. Same as Microsoft Word count.

Solution 16 - Javascript

This one-liner is pretty simple and counts words accurately even if there is more than one whitespace between them:

return string.split(/\s+/).length;
Regex Explanation
Part of Expression Explanation
\s Matches any whitespace character
+ Matches the previous token between one and unlimited times

Solution 17 - Javascript

Here's a function that counts number of words in an HTML code:

$(this).val()
    .replace(/((&nbsp;)|(<[^>]*>))+/g, '') // remove html spaces and tags
    .replace(/\s+/g, ' ') // merge multiple spaces into one
    .trim() // trim ending and beginning spaces (yes, this is needed)
    .match(/\s/g) // find all spaces by regex
    .length // get amount of matches

Solution 18 - Javascript

I'm not sure if this has been said previously, or if it's what is needed here, but couldn't you make the string an array and then find the length?

let randomString = "Random String";

let stringWords = randomString.split(' ');
console.log(stringWords.length);

Solution 19 - Javascript

function countWords(str) {
    str = str.replace(/(^\s*)|(\s*$)/gi,"");
    str = str.replace(/[ ]{2,}/gi," ");
    str = str.replace(/\n /,"\n");
    return str.split(' ').length;
}
document.write(countWords("  this function remove extra space and count the real   string lenth"));

Solution 20 - Javascript

You got some mistakes in your code.

function WordCount(str) {
    var totalSoFar = 0;
    for (var i = 0; i < str.length; i++) {
        if (str[i] === " ") {
            totalSoFar += 1;
        }
    }
    return totalSoFar + 1; // you need to return something.
}
console.log(WordCount("Random String"));

There is another easy way using regular expressions:

(text.split(/\b/).length - 1) / 2

The exact value can differ about 1 word, but it also counts word borders without space, for example "word-word.word". And it doesn't count words that don't contain letters or numbers.

Solution 21 - Javascript

I know its late but this regex should solve your problem. This will match and return the number of words in your string. Rather then the one you marked as a solution, which would count space-space-word as 2 words even though its really just 1 word.

function countWords(str) {
	var matches = str.match(/\S+/g);
	return matches ? matches.length : 0;
}

Solution 22 - Javascript

I think this answer will give all the solutions for:

  1. Number of characters in a given string
  2. Number of words in a given string
  3. Number of lines in a given string

 function NumberOf() { 
		 var string = "Write a piece of code in any language of your choice that computes the total number of characters, words and lines in a given text. \n This is second line. \n This is third line.";

		 var length = string.length; //No of characters
		 var words = string.match(/\w+/g).length; //No of words
		 var lines = string.split(/\r\n|\r|\n/).length; // No of lines

		 console.log('Number of characters:',length);
		 console.log('Number of words:',words);
		 console.log('Number of lines:',lines);


}

NumberOf();

  1. First you need to find length of the given string by string.length
  2. Then you can find number of words by matching them with string string.match(/\w+/g).length
  3. Finally you can split each line like this string.length(/\r\n|\r|\n/).length

I hope this can help those who are searching for these 3 answers.

Solution 23 - Javascript

If you want to count specific words

function countWholeWords(text, keyword) {
    const times = text.match(new RegExp(`\\b${keyword}\\b`, 'gi'));

    if (times) {
        console.log(`${keyword} occurs ${times.length} times`);
    } else {
        console.log(keyword + " does not occurs")
    }
}


const text = `
In a professional context it often happens that private or corporate clients corder a publication to be 
made and presented with the actual content still not being ready. Think of a news blog that's 
filled with content hourly on the day of going live. However, reviewers tend to be distracted 
by comprehensible content, say, a random text copied from a newspaper or the internet.
`

const wordsYouAreLookingFor = ["random", "cat", "content", "reviewers", "dog", "with"]

wordsYouAreLookingFor.forEach((keyword) => countWholeWords(text, keyword));


// random occurs 1 times
// cat does not occurs
// content occurs 3 times
// reviewers occurs 1 times
// dog does not occurs
// with occurs 2 times

Solution 24 - Javascript

You can use this algorithm :

app.js :

const TextArea = document.querySelector('textarea');

const CountContainer = document.querySelector('#demo');


TextArea.addEventListener('keypress', () => {
    let TextValue = TextArea.value.split(' ').join('-').split('\n').join('-').split('-');

    let WordCountArray = TextValue.filter(el => {
        return el != '';
    });

    let WordSen = WordCountArray.length <= 1 ? 'Word' : 'Words';

    console.log(WordCountArray);

    CountContainer.textContent = WordCountArray.length + ' ' + WordSen;

});

TextArea.addEventListener('keyup', function () {
    if (this.value === '') CountContainer.textContent = '0 Word';
});

HTML index page for test:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <textarea cols="30" rows="10"></textarea>
    <div id="demo"></div>

    <script src="app.js"></script>
</body>


</html>

Solution 25 - Javascript

Adapted from internals-in answer It handles the edge case ' ' as well

export const countWords = (str: string) => {

  str = str.trim();
  if (!str.length) {
    return str.length
  }
  return str.trim().split(/\s+/).length;
}

Jest tests

    test("countwords", () => {
        expect(countWords('  ')).toBe(0)
        expect(countWords('78   7 ')).toBe(2)
        expect(countWords('78 7 ')).toBe(2)
        expect(countWords('aa, , 7')).toBe(3)
        expect(countWords('aa, , \n \n \t 7 \n 4')).toBe(4)
    }); 

Solution 26 - Javascript

var str =   "Lorem ipsum dolor sit amet consectetur adipisicing elit. Labore illum fuga magni exercitationem porro? Eaque tenetur tempora nesciunt laborum deleniti, quidem nemo consequuntur voluptate alias ad soluta, molestiae, voluptas libero!" ;

let count = (str.match(/\s/g) || []).length;
console.log(count + 1 );

countWords =(str )=>{

    let count =  ( str.match(/\s/g)   || []  ).length;
    count = (count == 0 ) ? 0 : count +1  ; 
    return count 
}

Solution 27 - Javascript

This snippet will compute how many words are in the sentence:

let para = "hello world I am javascript";
console.log(para.split(" ").filter((x) => x !== "").length)

Solution 28 - Javascript

<textarea name="myMessage" onkeyup="wordcount(this.value)"></textarea>
<script type="text/javascript">
var cnt;
function wordcount(count) {
var words = count.split(/\s/);
cnt = words.length;
var ele = document.getElementById('w_count');
ele.value = cnt;
}
document.write("<input type=text id=w_count size=4 readonly>");
</script>

Solution 29 - Javascript

function totalWordCount() {
  var str ="My life is happy"
  var totalSoFar = 0;

  for (var i = 0; i < str.length; i++)
    if (str[i] === " ") { 
     totalSoFar = totalSoFar+1;
  }
  totalSoFar = totalSoFar+ 1; 
  return totalSoFar
}

console.log(totalWordCount());

Solution 30 - Javascript

function WordCount(str) {
    var totalSoFar = 0;
    for (var i = 1; i < str.length; i++) {
        if (str[i] === " ") {
            totalSoFar ++;
        }
    }
    return totalSoFar; 
}
console.log(WordCount("hi my name is raj));

Solution 31 - Javascript

You can use this simple way:

function wordCounter(text) {
  let arr = text.split('');
  return arr.length;
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser2755667View Question on Stackoverflow
Solution 1 - JavascriptBlenderView Answer on Stackoverflow
Solution 2 - Javascriptinternals-inView Answer on Stackoverflow
Solution 3 - JavascriptAlexView Answer on Stackoverflow
Solution 4 - JavascriptMr. PolywhirlView Answer on Stackoverflow
Solution 5 - JavascriptSeanView Answer on Stackoverflow
Solution 6 - JavascriptJeff VossView Answer on Stackoverflow
Solution 7 - JavascriptTimView Answer on Stackoverflow
Solution 8 - JavascriptWesleyACView Answer on Stackoverflow
Solution 9 - JavascriptneokioView Answer on Stackoverflow
Solution 10 - JavascriptRonen RabinoviciView Answer on Stackoverflow
Solution 11 - JavascriptPenny LiuView Answer on Stackoverflow
Solution 12 - Javascriptuser3743140View Answer on Stackoverflow
Solution 13 - JavascriptturnerView Answer on Stackoverflow
Solution 14 - JavascriptTimView Answer on Stackoverflow
Solution 15 - JavascriptSteView Answer on Stackoverflow
Solution 16 - JavascriptUnrealApexView Answer on Stackoverflow
Solution 17 - JavascriptRiki137View Answer on Stackoverflow
Solution 18 - Javascriptbrianna124View Answer on Stackoverflow
Solution 19 - JavascriptRohit NishadView Answer on Stackoverflow
Solution 20 - JavascriptAlosoView Answer on Stackoverflow
Solution 21 - JavascriptReality-TorrentView Answer on Stackoverflow
Solution 22 - JavascriptLiNView Answer on Stackoverflow
Solution 23 - JavascriptL. UrbanskiView Answer on Stackoverflow
Solution 24 - JavascriptMr AnonymousView Answer on Stackoverflow
Solution 25 - JavascriptChetan JainView Answer on Stackoverflow
Solution 26 - JavascriptSermet PekinView Answer on Stackoverflow
Solution 27 - Javascriptiamabs2001View Answer on Stackoverflow
Solution 28 - JavascriptSk MouryaView Answer on Stackoverflow
Solution 29 - JavascriptPiyusha PatelView Answer on Stackoverflow
Solution 30 - JavascriptRaj ChandakView Answer on Stackoverflow
Solution 31 - JavascriptnachoView Answer on Stackoverflow