By Kelajatu


2018-05-16 15:12:09 8 Comments

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?

5 comments

@Fabio Lolli 2018-05-16 15:16:28

If you just want an array of the lengths:

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

Should do the trick

@ponury-kostek 2018-05-16 15:32:55

According to MDN docs, arguments don't have map method so this is not valid implementation

@Fabio Lolli 2018-05-16 15:34:54

@ponury-kostek fixed, thanks for the tip!

@Ele 2018-05-16 15:28:51

Using the implicit object arguments you can get the keys, check for the attr length if a passed object doesn't have that attr.

const stringLengths = function() {
  return Object.keys(arguments).map(k => arguments[k].length || 0);
}

console.log(stringLengths('ele', 'from', 'stackoverflow', 2222));

@Grégory NEUT 2018-05-16 15:17:37

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',
));

@Fabio Lolli 2018-05-16 15:19:27

I liked this because you esplicitly flatten the arguments instead of using the arguments object. FYI, this works with arrow functions, arguments doesn't. I'd suggest this as best answer

@RogerC 2018-05-16 15:16:19

Your code works fine. You just need to start your loop with i=0 instead of 1, if you want all your arguments lengths.

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

  return arr;
}

console.log(stringLengths("hello", "okay", "doh"));

@Nope 2018-05-16 15:16:08

Your code is working fine, except you are starting at 1 instead of index 0. Arrays have a zero based index, hence I when you pass only 1 argument you get the initial empty array as the loop is not executed at all.

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

  return arr;
}

var x = stringLengths('123', '123456789', '12345');
console.log(x);

More Details on accessing array elements here

@Nope 2018-05-16 15:27:44

Just to add, arguments is not an Array but is array-like and is zero based. It only has the length property of an Array. It does not have shift or push for example. You can however use Array methods such as .map() and even the spread operator on it (...arguments). See one of the other answers for an example on that.

Related Questions

Sponsored Content

26 Answered Questions

[SOLVED] How to append something to an array?

34 Answered Questions

[SOLVED] How do I remove a property from a JavaScript object?

38 Answered Questions

[SOLVED] Check if object is array?

  • 2011-01-23 18:53:04
  • mpen
  • 1167624 View
  • 2076 Score
  • 38 Answer
  • Tags:   javascript arrays

35 Answered Questions

[SOLVED] Loop through an array in JavaScript

63 Answered Questions

[SOLVED] How do I remove a particular element from an array in JavaScript?

  • 2011-04-23 22:17:18
  • Walker
  • 4586479 View
  • 5798 Score
  • 63 Answer
  • Tags:   javascript arrays

40 Answered Questions

[SOLVED] How do I check if an array includes an object in JavaScript?

33 Answered Questions

[SOLVED] Sort array of objects by string property value in JavaScript

18 Answered Questions

[SOLVED] How do I empty an array in JavaScript?

  • 2009-08-05 09:08:39
  • amir
  • 1787765 View
  • 2199 Score
  • 18 Answer
  • Tags:   javascript arrays

32 Answered Questions

[SOLVED] Create ArrayList from array

23 Answered Questions

[SOLVED] For-each over an array in JavaScript?

Sponsored Content