Use Destructuring Assignment to Assign Variables from Objects Help

Use Destructuring Assignment to Assign Variables from Objects Help
0

#1

Tell us what’s happening:

It keeps giving me this:

destructuring with reassignment was used

What am I doing wrong?

Your code so far


function getLength(str) {
  "use strict";

  // change code below this line
  var length = {x:12};
  const  {x: len} = 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 (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

the value on the left of the colon has to be a valid property of the object you are destructuring.
and, the value on the right of the equal sign has to be the name of the object

In this case str is the object we want to destructure. We want to pull out the ‘length’ property and give it a new name ‘len’.

So re-read the example given on how to do that and hopefully with the additional information I just gave you, you can figure out how to solve this.


#3

Ok so I got this:

var length = {str:12};
const {str:len} = length;

and it still didn’t work. I don’t know what I’m doing wrong. I thought I reassigned it with destructing just like the example.


#4

Also this answer (talking about why it works the way it does)


#5

You still don’t seem to understand the example or my response.

You’ve put ‘length’ on the right side of the equal sign. Is ‘length’ the name of the object you are destructuring?
You’ve also put ‘str’ on the left of the colon : , is ‘str’ the name of a property found in the object you are destructuring?

You’ll need to pay attention to what is used on each side of the colon and each side of the equal sign. Where does the object name go? Where does the property go? Where does the new name go?
When you understand this (by either reading the above response or looking at the example given) you’ll get this one right.