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

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

#1

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:


#2

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-


#3

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


#4

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).


#5

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