Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

if statement not evaluating but console log shows it should

I am trying to write a function to get the length of a string without using the .length property nor any loops or built in methods. I am using recursion, however, the stopping statement is not evaluating and causing a stack overflow. I tried console logging string[i] and sure enough, once the length is reached, it console logs "undefined" but the if statement still won’t evaluate.

const getLength = (string, i = 0) => {
    if (string[i] === 'undefined') return 0;
    return 1 + getLength(string, i+1);
}

console.log(getLength("what is going on??")); //18

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

the if-state should be string[i] === undefined

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading