-3

I need to create a function that takes in four strings and returns an array containing the length of each string. I was given this:

function stringLengths(str1, str2, str3, str4) {
  //
}

I've created an array of strings and I've searched google for so long all words are starting to look the same.

Mohammad Usman
  • 37,952
  • 20
  • 92
  • 95

5 Answers5

6

You can use Rest Paremeters to convert arguments in to array and then map them to get your required result. Below is my snippet.

function stringLengths(...strings){
  return strings.map(string => string.length);
}
console.log(stringLengths("","something","something else"))
Maheer Ali
  • 35,834
  • 5
  • 42
  • 73
2

You can use rest syntax and .map() function to get an array of string's length:

const stringLengths = (...str) => str.map(({ length }) => length);

console.log(stringLengths('lorem', 'ipsum'));
console.log(stringLengths('lorem', 'ipsum', 'sit', 'amet'));
Mohammad Usman
  • 37,952
  • 20
  • 92
  • 95
1

If you don't want to change your function definition, you can also use the arguments object.

function stringLengths(str1, str2, str3, str4)
{    
    return [...arguments].map(x => x.length);
}

console.log(stringLengths("hello", "cruel", "world", "of", "darkness"));

Or like this:

function stringLengths(str1, str2, str3, str4)
{    
    return Object.values(arguments).map(x => x.length);
}

console.log(stringLengths("hello", "cruel", "world", "of", "darkness"));
Shidersz
  • 16,846
  • 2
  • 23
  • 48
0

There are excellent answers already to make stringLengths work with any number of parameters. However, all (so far) assume that each parameter is a string.

Which can lead to unexpected results:

  • Both arrays and functions have a length property
  • Any objects can be crafted with a length property

Or even errors:

  • Things like null or undefined will throw an error when attempting to read properties from them.

const stringLengths = (...strs) =>
  strs.map(s => s.length);

console.log(
  stringLengths(1, 2)
);

console.log(
  stringLengths({length: ''}, [''], (a, b) => '')
);

console.log(
  stringLengths(null, null)
);

You can write a decorator filterStrings which you can apply to stringLengths so that it gets only strings:

const stringLengths = (...strs) =>
  strs.map(s => s.length);

const filterStrings = fn => (...args) =>
  fn(...args.filter(arg => typeof arg === 'string'));
  
const safeStringLengths = filterStrings(stringLengths);

console.log(
  safeStringLengths(1, 2)
);

console.log(
  safeStringLengths({length: ''}, [''], (a, b) => '')
);

console.log(
  safeStringLengths(null, null)
);

console.log(
  safeStringLengths("foo", "bar", "foobar")
);

filterStrings is the function that actually gets all the parameters, it keeps only strings and pass them on to stringLengths.

Why do it this way?

It depends on your context really. The nice thing about the decorator is that it allows you to move the validation of the parameters outside of stringLengths and let the function do just one thing.

In some scenarios you can simply trust your parameters and having validation in your function would be useless. In other scenarios, you can't. Being able to combine functions allows you to keep things separated and organised.

customcommander
  • 17,580
  • 5
  • 58
  • 84
-3

here is your code

function stringLengths(str1, str2, str3, str4) { 
  return [str1.length, str2.length, str3.length, str4.length]
}

console.log(stringLengths('1','bb','ccc','dddd'))
Asim Khan
  • 1,969
  • 12
  • 21