0

When calling this function I am expecting myStrings to be ['A','B','C','D','E','F','G'] when in fact I'm seeing ['A', 'B', 'G']. When the recursive call is made a[i] equals [['C','D'],'E','F'] however the function receives the entire myArray. Any ideas?

var myArray = ['A','B',[['C','D'],'E','F'],'G'];

var myStrings = [];

function extractStrings(a) {

    for(var i=0; i<a.length; i++) {

        if(typeof a[i] === 'string') {
            myStrings.push(a[i]);
        } else if (typeof a[i] === 'object') {
            extractStrings[a[i]];
        }
    }
}

extractStrings(myArray);
console.log(myStrings);

1 Answer 1

4

It's a simple typo:

extractStrings[a[i]];

Should be:

extractStrings(a[i]);

Demonstration

Sign up to request clarification or add additional context in comments.

1 Comment

Grrr! Time to go to bed. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.