Destructuring Assignment to Assign object Variables

Tell us what’s happening:
question title ES6: Use Destructuring Assignment to Assign Variables from Objects, ask me to create use a destructuring with reassignment which i did but it wont let me pass. This happen to me more than once as well, idk what im doing wrong, the code works and is exactly as shown in example. My code is below take a look… thanks

Your code so far


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

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

    const{tomorrow : b} = AVG_TEMPERATURES; 
  const tempOfTomorrow = b;  
  // change code above this line
  return tempOfTomorrow;
}

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

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:65.0) Gecko/20100101 Firefox/65.0.

Link to the challenge:

You are not using the function parameter

You also need to do it all in one line, why are you using b? You don’t need it
It is not wrong per se, other than you have a variable that is not necessary, but the tests are checking for what you have exactly written

1 Like

so my code should look more like this

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

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

const{tomorrow : tempOfTomorrow} = AVG_TEMPERATURES;

// change code above this line
return tempOfTomorrow;
}

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

or maybe i am not understanding something ? by function parameter you are referring to ```
today: 77.5, & tomorrow: 79… and i used that in the example i just sent but its still letting me that im missing something

Within your function, that AVG_TEMPERATURES simply does not exist. Rewrite your code, without using that at all. There is something your function has that contains the same information, but you can’t rely on that global variable.

Can you see somewhere inside your function, some variable or parameter, that will contain those same values?

1 Like

Here when the function is called the object is passed in, you don’t need to use AVG_TEMPERATURES inside your function - you can’t if you want reusable code.

Hello.

First time round I did this (similar to redone’s solution) :

function getTempOfTmrw(avgTemperatures) {

    const {tomorrow} = AVG_TEMPERATURES
    const tempOfTomorrow = tomorrow 

    return tempOfTomorrow
}

and I got the right result (79) but when I clicked on ‘Run the tests’ I got:

// running tests
destructuring with reassignment was used
// tests completed

This sounded right to me (“destructuring with reassignment was used”) but at this point I realised that I hadn’t used the avgTemperatures parameter (as you (ieahleen) indicated) so I rewrote my code:

function getTempOfTmrw(avgTemperatures) {

    const {tomorrow} = avgTemperatures
    const tempOfTomorrow = tomorrow 

    return tempOfTomorrow
}

Again, this returned (79), the correct answer and again, after clicking ‘Run the tests’ I got the same message as before:

// running tests
destructuring with reassignment was used
// tests completed

and couldn’t click through to the next challenge.

I felt certain (before I ran the tests) that this would be the correct solution because the function is now flexible, e.g. if I created an identical object with the same name:

const REAL_TEMP = {
  today: 20,
  tomorrow: 100    
}

using exactly the same function, but calling it by passing the name of the new object as its argument:

console.log(getTempOfTmrw(REAL_TEMP))

I’d get the correct result (100).

As I said, the actual results are correct, so I imagine I can’t pass this challenge because of something
lacking in the syntax.

Could you give me another clue as to where I’m going wrong?
:slight_smile:

PS Just noticed that title of challenge page is “Destructuring assignment…” and message received after running tests mentions “Destructuring with reassignment” - not sure whether that is a typo or whether it’s pointing to my problem. (Googled “Destructuring with reassignment” but only got back “assignment” results).

The other thing you are missing is that you need to do it on one line

If you check the “destructuring with reassignment was used” is not checked - there is not uniformity in the tests about having written what you should do to pass the test, or what you shouldn’t do, so there is a bit of both, but it happens when many people make many changes to the same codebase

If you are asking why it doesn’t say so in the test, that should be on one line only… well, to know if you are accessing the property in the assigned way there is only one way: checking what you have written, as such there isn’t much freedom in how to solve this challenge

OK I got it!

Thanks for your help.