Use-destructuring-assignment-to-assign-variables-from-objects

Tell us what’s happening:

what is the mistake i have done in this coding ?? its not working help me

Your code so far



function getLength(str) {
  "use strict";

  // change code below this line
const len = str.length; 
  // change this
  // 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 (X11; Ubuntu; Linux x86_64; rv:58.0) Gecko/20100101 Firefox/58.0.

Link to the challenge:

You’re not using ‘destructuring’ ^^
I know you can assign the length of the string in an easy way as you did, but that’s not the assignment :stuck_out_tongue:
Try to read again the instruction (especially this example: const { x : a, y : b, z : c } = voxel ) then i’m pretty sure you will figure it out ^^

Hope it helps,
-LyR-

I don’t get it. The entire destructing thing is just confusing. Why ever do this rather than what @ravi6112 did

2 Likes

I had trouble thinking about this too, but helps to think of the string length as a property of the string object.

const stringObj = { key: value, key2:value, length:num}

The destructuring syntax refers to the object’s key (in this case to find the Length property) on the left side of the colon, and puts that in the new variable we want to create on the right of the colon (const len).

4 Likes

function getLength(str) {
“use strict”;

const {length: len} = str;

return len;

}

console.log(getLength(‘FreeCodeCamp’));

In this way you declare variable len as function for strings “length” and assign the value of str.length to len

12 Likes

Well if you didn’t give us the answer I would never have got that!
You might want to mark that with a spoiler?

A string is an object, it has its own properties, like length. What you need to do is the same thing as the line below:

“get the field x and copy the value into a,”

Notice that in var voxel = {x: 3.6, y: 7.4, z: 6.54 }; , voxel is the object to be destructured; where the identifier’s position should be in the destructuring assignment statement?

OK , for me at least this needs a bit of explanation.

SO, how is it that the “length” property does not require a str.length instead of just saying " length ? and
to me this looks like " len" is assigned as a value to " length " which is then assigned to “str” … so the circular logic would be that “str” is assigned to the “str” that is in the function argument ( a.k.a “FreeCodeCamp”

soooooo yea…

#teachMeLikeIm5

Can you please explain this process for me ?

2 Likes

Like futurethang wrote. Length is a property of the string object, so you don’t need to writing str.length as “length” itself does the job. If I’m not using it often I always forger how it works :stuck_out_tongue:

1 Like

Ok, very nice, now I need to fix a post-it note on my desk who says “always look at the object attributes before cursing FreeCodeCamp!!” I’m moving my Freakazoid picture to make room…

futurethang’s post is the one that really made me understand, this was the key for me and I assume for many people!

To further clarify (without giving away the answer outright) think of str in this case as an object:

str = { length: 12 } // If str is a different string, 12 should be replaced with the actual length of that string.

so in the str variable, ‘length’ is the key, and the actual length of the string is the value (in this example, the value is 12 because str is ‘freecodecamp’ which is 12 characters long).

Then compare to the voxel example to apply the destructuring. For me, I was able to solve it after thinking about str like this.

2 Likes

I also got stuck on this b/c I didn’t understand that any string is actually an object with property/value pairs. And one property automatically included in a string object is length. For a string, we don’t have to explicitly state the property/value pairs like we’re used to with other objects we create. Instead, it’s automatically done for us.

For example, the code:

// string to test
myStr = 'hey';

// display array of string's properties
function testStrAsObject(str) {
  console.log(Object.getOwnPropertyNames(str))
}

testStrAsObject(myStr);

outputs:

[ '0', '1', '2', 'length' ]

The code above displays all of a string’s properties in an array. The property length is automatically included. Also, as an aside, automatically included is a property for each character (who knew? not me).

So the string 'hey' can be viewed as:

var hey = {
    0 : 'h',
    1: 'e',
    2: 'y',
    length: 4
}

And that’s why getting a string’s length (i.e. hey.length) is really just accessing a value of an object property, which we’ve learned we can do using <object>.<property> syntax.

At least, I think I have that right. Can anyone concur or correct me?