Tell us what’s happening:
I’ve been working on the ES6 challenges and the one called “ES6: Use Destructuring Assignment to Assign Variables from Objects” took me a long time to grasp.
I think the problem description of this particular challenge, in contrast to the other ES6 challenges is not clear enough.
It is also confusing since length is JS property and it doesn’t have any education benefit in this example.
It seems that there has been confusion around this exercise before:
I’ve posted the exercise description and code editor starting code below. Can anyone understand?
Exercise description:
Consider the following ES5 code:
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
You may read it as “get the field x and copy the value into a,” and so on.
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’));
Use destructuring to obtain the length of the input string str, and assign the length to len in line.