Skip to content

Commit 22420e4

Browse files
committed
adds sentenceAnalyzer.js
1 parent c509c0c commit 22420e4

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

JS_Bits/sentenceAnalyzer.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
function getVowelCount(sentence) {
2+
const vowels = "aeiou";
3+
let count = 0;
4+
5+
for (const char of sentence.toLowerCase()) {
6+
if (vowels.includes(char)) {
7+
count++;
8+
}
9+
}
10+
return count;
11+
}
12+
13+
const vowelCount = getVowelCount("Apples are tasty fruits");
14+
console.log(`Vowel Count: ${vowelCount}`);
15+
16+
function getConsonantCount(sentence) {
17+
const consonants = "bcdfghjklmnpqrstvwxyz";
18+
let count = 0;
19+
20+
for (const char of sentence.toLowerCase()) {
21+
if (consonants.includes(char)) {
22+
count++;
23+
}
24+
}
25+
return count;
26+
}
27+
28+
const consonantCount = getConsonantCount("Coding is fun");
29+
console.log(`Consonant Count: ${consonantCount}`);
30+
31+
function getPunctuationCount(sentence) {
32+
const punctuations = ".,!?;:-()[]{}\"'–";
33+
let count = 0;
34+
35+
for (const char of sentence) {
36+
if (punctuations.includes(char)) {
37+
count++;
38+
}
39+
}
40+
return count;
41+
}
42+
43+
const punctuationCount = getPunctuationCount("WHAT?!?!?!?!?");
44+
console.log(`Punctuation Count: ${punctuationCount}`);
45+
46+
function getWordCount(sentence) {
47+
if (sentence.trim() === '') {
48+
return 0;
49+
}
50+
51+
const words = sentence.trim().split(' ');
52+
let count = 0;
53+
54+
for (const word of words) {
55+
if (word !== '') {
56+
count++;
57+
}
58+
}
59+
60+
return count;
61+
}
62+
63+
const wordCount = getWordCount("I love freeCodeCamp");
64+
console.log(`Word Count: ${wordCount}`);

0 commit comments

Comments
 (0)