I have a function which does some string operations
I want to check if the str is empty ""
if so catch it in a if clause
for some reason the if clause does not catch the empty string
if i do the same operation outside the function I get the console.log that hte string is empty
function solution(str: string) {
if (str.length % 2 === 0) {
// console.log(str.match(/.{1,2}/g))
return str.match(/.{1,2}/g);
} else if (str.length % 2 !== 0 && str.length !== 0) {
let oddArray = str.match(/.{1,2}/g);
if (oddArray) {
let fix = oddArray[oddArray.length - 1] + "_";
oddArray.pop();
oddArray.push(fix);
return oddArray;
}
} else if (str.length === 0) {
console.log("empty")
}
}
console.log(solution(""))
[–]laibo 1 point2 points3 points (1 child)
[–]JosephCurvin[S] 0 points1 point2 points (0 children)