Return an array with the length property of arguments passes

  • A+
Category:Languages

Currently Stuck on this challenges.

// stringLengths takes in four strings // it returns an array containing the length of each string // Example: stringLengths('mushroom', 'onion', '', 'garlic') returns [8, 5, 0, 6]   function stringLengths(str1, str2, str3, str4) {  } 

Curently have

function stringLengths(str1, str2, str3, str4) {     var arr = [];     for (var i = 1; i < arguments.length; i++) {         arr.push(arguments[i].length);     }      return arr; } 

This return me an empty array.

How can I push the length property of of each arguments into the Array and return the array with the lengths of each?


You can use of ... and Array.map which are ES6 features. So you will handle any number of string.

function stringLengths(...args) {   return args.map(x => x.length); }  console.log(stringLengths(   'str1',   'je suis un chocolat',   'les croissants sont bons', ));


More concise

const stringLengths = (...args) => args.map(x => x.length);  console.log(stringLengths(   'str1',   'je suis un chocolat',   'les croissants sont bons', ));

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: