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

153
Views
How to return an array of numbers that represent lengths of elements string?

If an array was: ['hey', 'you', 'muddy'] The expected output should be: [3, 3, 5]

This is what I have so far:

function lengths(arr) {
  numbersArray = [];
  for (var i = 0; i < arr.length; i++) {
    numbersArray = arr[i].length;
  }
}

Any help would be much appreciated.

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

0

You need to push the length of every item (using Array#push) and return the array in the end:

function lengths(arr) {
  const numbersArray = [];
  for (let i = 0; i < arr.length; i++) {
    numbersArray.push(arr[i].length);
  }
  return numbersArray;
}

console.log( lengths(['hey', 'you', 'muddy']) );

Another solution using Array#map:

function lengths(arr) {
 return arr.map(str => str.length);
}

console.log( lengths(['hey', 'you', 'muddy']) );

about 4 years ago · Juan Pablo Isaza Report

0

const getStringLength = (arr) => {
  return arr.map((item) => item.length);
}
console.log(getStringLength(['hello', 'world']));

about 4 years ago · Juan Pablo Isaza Report

0

function lengths(arr) {
  return arr.map(el => el.length)
}

Array.map is commonly used in such things. Docs

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!