Tell us what’s happening:
Your code so far
function getLength(str) {
"use strict";
// change code below this line
const length = 0; // 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 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/604.5.6 (KHTML, like Gecko) Version/11.0.3 Safari/604.5.6
.
Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/es6/use-destructuring-assignment-to-assign-variables-from-objects/
What do you not understand about it?
What have you have tried?
I understand how we can destructure the object and arrays but confused how we can destructure object, if we are using the function.
var voxel = {x: 3.6, y: 7.4, z: 6.54 };
var x = voxel.x; // x = 3.6
var y = voxel.y; // y = 7.4
var z = voxel.z; // z = 6.54
Here’s the same assignment statement with ES6 destructuring syntax:
const { x, y, z } = voxel; // x = 3.6, y = 7.4, z = 6.54
If instead you want to store the values of voxel.x into a, voxel.y into b, and voxel.z into c, you have that freedom as well.
const { x : a, y : b, z : c } = voxel // a = 3.6, b = 7.4, c = 6.54
Till here I understand.
Strings have prototype methods built in called length.
You are simply assigning length into a new variable called len.
const { x : a, y : b, z : c } = voxel // a = 3.6, b = 7.4, c = 6.54
Use this as an example.
function getLength(str) {
"use strict";
// change code below this line
const length = 0; // change this
// change code above this line
const len= str.length; // you must assign length to len in line
}
console.log(getLength('FreeCodeCamp'));
don’t understand.
What about this?
var str = {length:...}
Then what should go in here?
const length = 0; // change this
Hint: Use deconstruction using const here.
Thank you so much I will try to do that.
No, I just gave my above reply as an example.
Can you try now?
I got the solution but I don’t understand the concept it’s very confusing.