Reputation: 5671
I should write a function that removes vowels from a string. I get an error message about null value. After several tries for fixing it, the message is this same, but I tried to filter null values.
TypeError: Cannot read property 'length' of null at getCount at Test.it at Test.describe at Object.exports.runInThisContext
function getCount(str) {
var vowelsCount = 0;
if (str && str.length){
vowelsCount=str.match(/[aeiou]/gi).length;
} else {
vowelsCount=0;
}
return vowelsCount;
}
describe("Case 1", function(){
it ("should be defined", function(){
Test.assertEquals(getCount("abracadabra"), 5)
});
});
Upvotes: 0
Views: 4493
Reputation: 814
Maybe like this ?
function getCount(str) {
var vowelsCount = 0;
if (str && str.length){
var m = str.match(/[aeiou]/gi)
if (m) return m.length;
} else {
vowelsCount=0;
}
return vowelsCount;
}
describe("Case 1", function(){
it ("should be defined", function(){
Test.assertEquals(getCount("abracadabra"), 5)
});
});
Upvotes: 2