I cannot complete clause: 'destructuring with reassignment was used'

Tell us what’s happening:
Hello, I stuck on this task, please help in what Am I wrong?

I can not complete clause: ‘destructuring with reassignment was used’

Your code so far


const AVG_TEMPERATURES = {
  today: 77.5,
  tomorrow: 79
};

function getTempOfTmrw(avgTemperatures) {
  "use strict";
  // change code below this line

  var {tomorrow} = avgTemperatures;
  const tempOfTomorrow = tomorrow;  // change this line

  // change code above this line
  return tempOfTomorrow;
}

console.log(getTempOfTmrw(AVG_TEMPERATURES)); // should be 79

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 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

  var {tomorrow} = avgTemperatures;
  const tempOfTomorrow = tomorrow;  // change this line

You don’t need to 2 lines here. You can assign tomorrow to tempOfTomorrow on the same first line.

Try to use this syntax. In deconstruction, you are assigning values from left to right divided by colon.

const { x : a, y : b, z : c } = voxel // a = 3.6, b = 7.4, c = 6.54

2 Likes

you should destructure and reassign the variable in the same line think of it as if you are renaming the destructured variable and do as the mentioned examples .

I’m getting the first check but not the second.

I can get the right output on this one with the code on one line using the syntax shimphillip posted ( const { x : a } = voxel ) but I can’t get the check for destructuring with reassignment was used.

can you share you edited code ?

This is what I have. I think it’s right but if it’s not it might be close enough to give the answer away so I blurred it.

const { tomorrow : tempOfTomorrow } = AVG_TEMPERATURES;

With just that one line between the change code below and above this line comments I can’t get the second check.

My output is
// running tests
destructuring with reassignment was used
// tests completed

2 Likes

This problem has already been solved : ES6: Use Destructuring Assignment to Assign Variables from Objects SEPT18
You are using directly the name of your tested object (AVG_TEMPERATURES) within your function, which make it contingent. You should put the name of your argument instead (avgTemperatures) so your function can be used with several “temperature” objects that have a tomorrow value.

22 Likes

You’re almost there. The syntax is for destructuring is correct so…

…you are desctucturing the right way

…but you are destructuring the wrong thing. Should be the object passed into function as parameter…

6 Likes
const AVG_TEMPERATURES = {
  today: 77.5,
  tomorrow: 79
};

function getTempOfTmrw(avgTemperatures) {
  "use strict";
  // change code below this line

  const {tomorrow : tempOfTomorrow} = avgTemperatures;  // change this line

  // change code above this line
  return tempOfTomorrow;
}

console.log(getTempOfTmrw(AVG_TEMPERATURES)); // should be 79
3 Likes

This is the solution I have given:

const { today : tempOfToday ,tomorrow : tempOfTomorrow } = avgTemperatures;

I am getting the output 79. But still getting the “destructuring with reassignment was used” message. Can someone help?

You only need the tomorrow in the destructuring assignment, not today

But doesnt hurt the destructuring does it. Even if I put only tomorrow I am getting the same error. Same for the nested destructuring as well.

const { tomorrow : { max : maxOfTomorrow }} = forecast ;

Same message I am getting for nested destrucuring as well

Not sure of the issue here, I’d need to see your full code

const LOCAL_FORECAST = {
today: { min: 72, max: 83 },
tomorrow: { min: 73.3, max: 84.6 }
};

function getMaxOfTmrw(forecast) {
“use strict”;
// change code below this line
const { tomorrow : { min : minOfTomorrow, max : maxOfTomorrow}} = forecast ; // change this line
// change code above this line
return maxOfTomorrow;
}

console.log(getMaxOfTmrw(LOCAL_FORECAST)); // should be 84.6

The output is 84.6…I am getting it…Along with the message “nested destructuring was used”.

Remove the minOfTomorrow - the tests actually check the code written, not just the output, and including it causes the tests to fail. There is nothing incorrect w/r/t to your code, but the tests are finicky. Once removed, the test passes - try clearing your browser cache and resubmitting the test if it doesn’t work straightaway.

Clearing the browser cache actually worked…Thanks Dan :v:

1 Like

I used object name instead of parameter :flushed: but thanks to you I figured it out!

2 Likes

This is the solution.
Inoder to access the value of a property in an object using the destructuring assignment, you can use this syntax.
const {property name: variable name} = object name or the parameter passed in the function;
So, this is my code to the solution:

const AVG_TEMPERATURES = {
today: 77.5,
tomorrow: 79
};

function getTempOfTmrw(avgTemperatures) {
“use strict”;
// change code below this line
const {tomorrow:tempOfTomorrow} = avgTemperatures; // change this line

// change code above this line
return tempOfTomorrow;
}

console.log(getTempOfTmrw(AVG_TEMPERATURES)); // should be 79

See the function argument properly. Assign the passed argument not the actual object.
AVG_TEMPERATURES is object.
avgTemperatures is argument of function.

Almost 6 months ago I know :stuck_out_tongue: but I’m with you, the error should be something in the lines of “too many variables assigned” just because the deconstructing was correctly implemented. In the examples there were no mentions if you need to assign all the variables or just one, and including an extra variable seems natural, specially if you’re copying the given example.

1 Like