-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path19-wordCountString.js
More file actions
39 lines (31 loc) · 981 Bytes
/
19-wordCountString.js
File metadata and controls
39 lines (31 loc) · 981 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// count the number of words in a string
function wordCount(str) {
let count = 0;
for (let i = 0; i < str.length; i++) {
if (str[i] == ' ') {
count++;
}
}
count++;
return count;
}
let myString = 'My name is Jisan. I am a programmer'
//console.log(wordCount(myString));
//another small solution
function countWord(str) {
// split method will spearte all the word by comma and turn the string into an array then we will get the lenght of the array using .lenght;
return str.split(' ').length;
}
console.log(countWord(myString));
//acurate way
function count_words(str) {
//exclude start and end white space
str = str.replace(/(^\s*)|(\s*$)/gi, "");
//convert 2 or more spaces to 1
str = str.replace(/[ ]{2,}/gi, " ");
//exclude newline with a start spacing
str = str.replace(/\n /, "\n");
return str.split(' ').length;;
}
myString = "My name is Jisan."
console.log(count_words(myString));