Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

147
Views
Is it possible to use closure and recursive approach for nested arrays in Javascript

I need to count vowels in nested array, and I'd like to do it with closure to avoid global namespace pollution. Here's my code:

let nestedArr = [
    "Elie",
    ["Matt", ["Tim"]],
    ["Colt", ["Whiskey", ["Janey"], "Tom"]],
    "Lorien"
];


function countVowels() {
    let vowelsCount = 0;
    let vowels = ['a', 'e', 'i', 'o', 'u'];
    return function foo(arr) {
        for (let i = 0; i < arr.length; i++) {
            if (typeof arr[i] === 'string') {
                for (let letter of arr[i]) {
                    if (vowels.includes(letter.toLowerCase())) {
                        vowelsCount++;
                    }
                }
            } else {
                return foo(arr[i]);
            }
        }
        return vowelsCount;
    }
}

const counter = countVowels();
console.log(counter(nestedArr));

I expect correct number of vowels, but get 5. I tried to debug and see it just stops after "Tim" which is the deepest subarray, so obviously my function does not go level up and I am missing something.

How can I achieve this?

Thank you in advance.

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You don't need the nested function, you can declare a single recursive function and still keep everything self-contained.

const countVowels = (arr) => {
  const vowels = ['a', 'e', 'i', 'o', 'u'];
  const vowel_count = (s) => [...s].filter((c) => vowels.includes(c.toLowerCase())).length;

  let vowels_total = 0;

  for (const e of arr) {
    vowels_total += Array.isArray(e) ? countVowels(e) : vowel_count(e);
  }

  return vowels_total;
};

const nestedArr = ['Elie', ['Matt', ['Tim']], ['Colt', ['Whiskey', ['Janey'], 'Tom']], 'Lorien'];

console.log(countVowels(nestedArr));

Or, foregoing recursion with a curried closure using Array#flat() (to Infinity)

const countVowels = (
  (v) => (arr) =>
    [...arr.flat(Infinity).join('')].filter((c) => v.includes(c.toLowerCase())).length
)(['a', 'e', 'i', 'o', 'u']);

const nestedArr = ['Elie', ['Matt', ['Tim']], ['Colt', ['Whiskey', ['Janey'], 'Tom']], 'Lorien'];

console.log(countVowels(nestedArr));

about 4 years ago · Juan Pablo Isaza Report

0

Your function works just fine if you just change

return foo(arr[i]);

To:

foo(arr[i]);

You need to let the entire loop run (obviously), the return makes it stop earlier.

let nestedArr = [
    "Elie",
    ["Matt", ["Tim"]],
    ["Colt", ["Whiskey", ["Janey"], "Tom"]],
    "Lorien"
];


function countVowels() {
    let vowelsCount = 0;
    let vowels = ['a', 'e', 'i', 'o', 'u'];
    return function foo(arr) {
        for (let i = 0; i < arr.length; i++) {
            if (typeof arr[i] === 'string') {
                for (let letter of arr[i]) {
                    if (vowels.includes(letter.toLowerCase())) {
                        vowelsCount++;
                    }
                }
            } else {
               foo(arr[i]);
            }
        }
        return vowelsCount;
    }
}

const counter = countVowels();
console.log(counter(nestedArr));

about 4 years ago · Juan Pablo Isaza Report

0

This mostly sounds like an exercise to learn recursion. If so, I leave you to the other fine recursive answers here.

But a simple alternative is to note the toString format for Arrays, and realize that we can simply apply our tests to the result of that, leaving some very simple code:

const countVowels = (a) =>
  [... a .toString () .toLowerCase ()] .filter (c => 'aeiou' .includes (c)) .length

const nestedArr = ['Elie', ['Matt', ['Tim']], ['Colt', ['Whiskey', ['Janey'], 'Tom']], 'Lorien']

console .log (countVowels (nestedArr))

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!