Other ways to complete this - Destructuring Assignment to Assign Variables from Objects

Other ways to complete this - Destructuring Assignment to Assign Variables from Objects
0.0 0

#1

Tell us what’s happening:

Is there another way to do this? Thanks. (I passed the challenge)

Your code so far


function getLength(str) {
  "use strict";

  // change code below this line
  const length = str.getLength;
  const {length: len} = str; // change this
  // change code above this line

  return len; // you must assign length to len in line

}

console.log(getLength('FreeCodeCamp'));

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/es6/use-destructuring-assignment-to-assign-variables-from-objects


#2

You don’t need this line. When you destructure and then reassign, the first name is the actual variable you are destructuring, and the second is how you wish to rename it. So

// length is destructured from the value str.length, then renamed to len
const {length: len} = str;
 
// return the renamed variable len
return len

Also, since you have a passing result, can you wrap your code with [spoiler] tags. The closing one needs to be /spoiler. This way people won’t see the answer by accident.


#3

I never would have guessed that the first name refers to the property of the variable we are destructuring. So we should suggest that a property-named variable in relation to the Type of the variable we are destructuring is suggestive of a property(in this case the length property) of that destructured variable correct? And thanks for your response :pray: