Am failing one test case. And am not sure if am going about this challenge the right way. Any help is much apprecitaed.
Failed test: “destructuring with reassignment was used”
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/67.0.3396.99 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
Usually the solutions are not that hard what you might think
Remember, that a string is an object, and as such it has some properties, length
being one of them.
So all you need to do is to access them. Your thinking is in the right direction but think again of destructuring. When you declare a new variable, you want to assign the value of a property
or a method
of the object
to your defined variable. In pseudo-code it would read like this
Let the value of property/method
be assigned to myVariable
when that property
is stored inside the object
.
I really hope that’ll help. It’s a one line of code actually excluding return
. Let me know, bro
Thank you, for making the concept clearer.
I changed my code. It still fails the same test case.
function getLength(str) {
"use strict";
// change code below this line
const {length:length}=str; // change this
// change code above this line
return length; // you must assign length to len in line
}
console.log(getLength('FreeCodeCamp'));
I’ve got it, thanks.
Just needed to change one variable.
const {length:len}=str;