Destructuring the length property

Destructuring the length property
0

#1

Tell us what’s happening:

what next ?

Your code so far


function getLength(str) {
  "use strict";

  // change code below this line
  const length =str.length;   // change this
  const len = length;
  // change code above this line

  return len; // you must assign length to len in line

}

console.log(getLength('FreeCodeCamp'));

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/es6/use-destructuring-assignment-to-assign-variables-from-objects


#2

Well, the instructions ask you to use destructuring. Where in your solution are you doing that?

Hint: You may not realize this, but strings do have properties and thus can be treated as objects. If you think of str as an object, how could you use destructuring to assign it’s length property to len?


#3

@RandellDawson
I am confusing about destructuring because they use one kind example and ask me to do another type.


#4

You can reference str as an object, because it does have properties (like length). This challenge does not really use the best choice for destructuring an object, because up till now you think of objects looking like:

var obj = {
  prop1: 'value1',
  prop2: 'value2';
}

#5

This is a little bit tricky because it seems like you are dealing with a variable & a string, but when using length or any other method or property, you are also dealing with an object.

Here you can apply the ES6 destructuring assignment…

You may want to see this hint which will probably give you an idea to complete this challenge.


#6

Mainly I am confusing to define the “len” and assign it to the length. that’s the problem.


#7

I also got stuck with this for an hour, but then I found the solution thanks to the previous hint I shared with you.

Don’t give up. (It’s the same reasoning like in the examples. The only difference and the key is that you should know that you are dealing with an object, not just — like the first lessons teach — a simple variable holding a string.)


#8

thanks for the tip. I got it.


#9

Yeah, that is a bad problem. We don’t think of strings as objects, so how can you use destructuring? I think when JS sees that you are trying to use it as an object, it coerces it to the String object type so that you can do it.

It’s a bad example because it involves knowledge that hasn’t been discussed. I’ll add it to the list of things to bring up in the curriculum.