Tell us what’s happening:
Hi, struggling to get this to return 0 when sentence = “”
I’ve tried using typeof (i.e if it isn’t a string returning 0 or if it is undefined returning 0)
Any help much appreciated!
(You can see various of my attempts in this code- I have been looking at it too long and am stepping away)
Your code so far
function getVowelCount(sentence) {
const vowels = "aeiou";
let count = 0;
for (const char of sentence.toLowerCase()) {
if (vowels.includes(char)) {
count++;
}
}
return count;
}
const vowelCount = getVowelCount("Apples are tasty fruits");
console.log(`Vowel Count: ${vowelCount}`);
function getConsonantCount(sentence) {
const consonants = "bcdfghjklmnpqrstvwxyz";
let count = 0;
for (const char of sentence.toLowerCase()) {
if (consonants.includes(char)) {
count++;
}
}
return count;
}
const consonantCount = getConsonantCount("Coding is fun");
console.log(`Consonant Count: ${consonantCount}`);
function getPunctuationCount(sentence) {
const punctuations = ".,!?;:-()[]{}\"'–";
let count = 0;
for (const char of sentence) {
if (punctuations.includes(char)) {
count++;
}
}
return count;
}
const punctuationCount = getPunctuationCount("WHAT?!?!?!?!?");
console.log(`Punctuation Count: ${punctuationCount}`);
// User Editable Region
function getWordCount (sentence){
if (typeof(sentence) === "undefined"){
return 0
}
let length = sentence.length
const letters ="abcdefghijklmnopqrstuvwxyz"
for (const char of sentence.toLowerCase()){
if (!letters.includes(char)){
return 0
}
else
{let count = sentence.trim().split(/\s+/).length;
return count
}
}}
console.log(getWordCount(""))
// User Editable Region
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/148.0.0.0 Safari/537.36
Challenge Information:
Build a Sentence Analyzer - Step 7