Use Destructuring Assignment to Assign Variables from Objects v

Use Destructuring Assignment to Assign Variables from Objects v
0

#1

Tell us what’s happening:

Your code so far


function getLength(str) {
  "use strict";

  // change code below this line
  const length = { x : str.length}; // change this
  // change code above this line
 const  { x: len } = length;
  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 10.0; 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

You have a string named str
You want to use desctructuring assignment to expose the string’s length property
so you can refer to it by len, not str.length.

The destructuring part should happen on left side of the =
str already has a length property so you don’t need to make it again
When renaming a property the new name would be the one to the right of the :


#3

Think of the problem like this:

const {objectPropertyName : newDelcaredVariableName } = object;

Use what @alhazen1 has told you above to figure out what to put where.


#4

Hey I had to create this account to answer to you, i had the same doubt you had at the same time as me and i just figure it out… I would say that the part the ppl that are following FCC have a problem with is realizing that a ‘string’ doesn’t actually exists. A string is basically an object with some intrinsic properties, one of them being length.