Hello Fellow Coders!
I’m going through the Javascript practice; Arrays, Loops, Objects, Iterators and I’m perplexed on how come the
if (amountChar < 2){ return 0; }
needs to be in its block scope?
this is the code ::::
const subLength = (str, char) => {
let amountChar = 0;
let len = -1;
for(let i = 0; i < str.length; i++) {
if (str[i] === char){
amountChar++;
if (amountChar > 2){
return 0;
}
if (len == -1){
len = i;
} else {
len = i - len + 1;
}
}
}
> * if (amountChar < 2){
> * return 0;
}
return len;
}
Why is the if (amountChar < 2){ return 0; }
statement not allowed to come after…
if (amountChar > 2){ return 0; }
??
Can anyone answer this cos this has bugged me for a couple of days now?
[JavaScript Practice: Arrays, Loops, Objects, Iterators]