Help me about destruction assignment

Help me about destruction assignment
0

#1

function getLength(str) {
“use strict”;

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

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

}

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


#2

Can you format your code and add a link to the exercise?


#3

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

thats the link to the challenge


#4

Hey @zeshan880,
Javascript is an object oriented language.
So, even the string is an object in javascript.
Now, think of string as any normal object and length as its one of the properties.

var string  = {
   "length" : 6
}

Now read the instruction in the challenge carefully.
They want this length property of str object to be assigned to the len variable.
Try solving now.
Does that help?


#5

Hello!

Think about len as an object, if it is an object then, it has properties, in this case your object const len has 1 property that should be length so, remember from previous lessons how to assign a value to an object property, that is:

obj.property = “value”

Implement the same but with the challenge being:
const len => Object
length => property of “len” object
str.length => value to assign to len.length

Hope this helps!


#6

i cant solve it i had tried a lot


#7

Could you post your latest code?


#8

This should work:

// change code below this line

const { length: len } = str; // change this

// change code above this line

After reading the challenge, you should have noted that you are using destructing variables assignment. The code above can translate to this:

Assign the value of the property length to the variable len searching from the object str.

Hope this helps!


#9

Your code has been blurred out to avoid spoiling a full working solution for other campers who may not yet want to see a complete solution.


#10

Hi,

Sorry about that, didn’t know it could not be posted.


#11

It is just preferred for the FCC challenges, to give guidance and give the OP a chance to figure it out on his/her own.