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

this is the working code:
onst 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

The exercise, the “correct” answer and the feedback the exercise provides on “wrong” answers are all dumb. What’s worse, is that we can’t mark an exercise as faulty, or suggest improvements directly from their pages.

  1. What’s the point of asking for tomorrow’s average temperature when you only have one temperature at hand?
  2. Why is an answer wrong when it outputs the required answer and nothing else, no errors either?
  3. If there’s a specific correct answer expected, why isn’t it hinted at the user?

P.S. The above are all rhetorical questions, simply meant as feedback, just in case someone at FCC actually notices. If that happens, please allow us to leave some feedback on the exercises.

You can open an issue in GitHub if there is not one already on the matter

FreeCodeCamp is user powered a lot of work is done by users, you are welcome to contribute yourself

Yes, the wording could definitely be improved, as could the test cases, and it could be made clearer that the object used to test the code is an object used to test the code. But take for example:

// Squaring the value of TWO should return four
const TWO = 2;

function square(num) {
  // your code here
}

This gives the correct answer:

function square(num) {
  TWO * TWO
}

But it’s should be quite clear that it’s not the correct answer. square is a function, and that function takes an argument. The answer should be one of:

function square(num) { return num * num; }
function square(num) { return num ** 2; }
function square(num) { return Math.pow(num, 2); }

It is reasonable to expect that someone who has been exposed to JS understands that functions take arguments and do something to those arguments, and return a value. Just to reiterate:

I don’t think this is really a good way to look at things: almost every single task in FCC can be made to pass the tests by simply returning exactly what the tests require. And this extends to programming in general: you don’t need to write any real logic, you can just write exactly what the output should be. But this defeats the point of using programming in the first place. As an example, this passes all the tests for the Reverse a String algorithm challenge:

function reverseString(str) {
  if (str == "hello") return "olleh";
  if (str == "Howdy") return "ydwoH";
  if (str == "Greetings from Earth") return "htraE morf sgniteerG";
}

Again, it should be quite clear that although it passes the tests, it is at the very least an extremely bad solution.


EDIT:

The example is contrived but the basic structure is completely realistic. You have an object (maybe returned from a weather API). You want to write a function that takes that object and pulls out certain values (in reality, maybe doing some computation on them).

I am not sure quite what you mean by having only one temperature on hand, but in the challenge, that object that is passed in could be anything as long as it has a tomorrow property. In the case of the challenge, there is a test object that does have that tomorrow property, and it is used to check whether the code you write is correct.

I would say that the mooted change to the curriculum, making it completely project-based, should remove a lot of the ambiguety that exists on challenges like this by making them actually deal with real API responses, and it should maybe be a bit clearer that a learner is doing the wrong thing.

Currently, this is the exercise:

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

function getTempOfTmrw(avgTemperatures) {
  "use strict";
  // change code below this line
  const tempOfTomorrow = undefined; // change this line
  // change code above this line
  return tempOfTomorrow;
}

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

And this is the requirement:

Use destructuring to obtain the average temperature for tomorrow from the input object AVG_TEMPERATURES , and assign value with key tomorrow to tempOfTomorrow in line.

I meant that there’s only one temperature for “tomorrow”, and a quick read can easily mislead people that apart from the destructuring, a mathematical average would also be expected to be calculated, but there’s only one temperature to work with. Yes, a careful read should make this clear for everyone, but I think that especially with such a technical kind of work it’s best if we make sure we have as few ambiguities as possible.

As for the right and wrong answer, take a look at this technically correct answer that is currently rejected:

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

Also correct, currently rejected:

const { today, tomorrow: tempOfTomorrow } = avgTemperatures;

Of course… if we don’t use a variable, why declare it at all? But this is something that could be tested and give the user proper feedback. But then how much sense does it make to return anything else than avgTemperatures.tomorrow? See, this exercise needs a lot of attention because it’s not aimed at people who already know what destructuring is, how it works, and that it’s not mandatory to use all the keys. And there’s no link to some JS documentation about ES6 destructuring.

And it should be easy to offer feedback directly from each exercise.

I agree with you, and I think personally that the whole ES6 section is borked. The sheer number of PRs open on the FCC codebase (3.4k atm!) make any attempt to fix issues extremely difficult (& imo making feedback easier, not requiring opening an issue on GH, would likely make this issue much worse in current setup).

But one of the issues with testing (and maybe this is a fundamental flaw in this part of the curriculum) is that there is no difference between this:

return avgTemperatures.tomorrow;

And this:

const { tomorrow: tempOfTomorrow } = avgTemperatures;
return tempOfTomorrow;

So the only way to check the code is to literally read the text of it line by line, via regex; the tests cannot just check input => output, so it’s difficult to write effective tests. Plus, putting that aside, the tests have to run quickly: they could take and parse the code and use some kind of injection to spy on the inner workings, but that’s going to be a non-starter because the suite has to run instantaneously. There needs to be a small amount of tests that are as simple as possible (else already-difficult maintainance becomes almost impossible)

1 Like

Yep, I was following that; :crossed_fingers::crossed_fingers::crossed_fingers:

Yeah, that sounds pretty good. Even with that it’ll still be pretty brutally difficult getting getting the balance right. Like, the challenges can be written to make them easier to test, but that then causes them to drift toward more contrived territory, which then causes its own issues [eg the function wrappers in the ES6 section], and so on… Heh, and I’ve been going and checking over issues every time I’ve found something I don’t like, and basically anything I’ve had an issue with has had either an issue raised or it’s gone to PR. Hopefully the project-based curriculum will help, if it gets implemented; it would remove the need to contrive standalone code (which often lacks context for learners)

1 Like

I know it isn’t easy to test the code, but there are two things that can be improved without even touching the validation code:

  1. better phrase the requirements and even the sample code;
  2. when the output is correct, offer some hints about why it isn’t validated and/or what it is expected from the solution.

To be frank, I had no issues with the ES6 section until I ran into this exercise. Even the HTML+CSS area was very good overall, and the tiny issues I encountered were easy to overcome. But this one was by far the most ambiguously presented. Had the introductory information been more explicit about how the destructuring works, and that it only makes sense to destructure to the variables that you will need later, that alone would’ve been a great hint. Something like:

Please note that in programming it is considered a waste of resources to declare variables that you never use, so the destructure of an object also allows you to specify only the keys that you later need in the code.

The requirement of this exercise could’ve also provided this information, instead of one sentence, or a validation that doesn’t provide any kind of feedback that could be used to reach the accepted answer(s) to this problem.

As for gathering feedback, maybe present a list of choices that the users would vote on, and only index the counters for each issue:

  1. I don’t understand the provided documentation about the topic;
  2. I don’t fully understand the requirements;
  3. The sample code is ambiguous;
  4. My answer is correct but not accepted (and in this case the user’s code could also be submitted to FCC for evaluation).
  5. etc

Also vital is to provide links to documentation, because Get a hint doesn’t help at all in this case (and maybe many others - I never requested hints until I bumped into this exercise, so I don’t know how the hints are), and Ask for help helps considerably less than a link to a good documentation. Maybe the easiest way to help the users would be to have good hints.

P.S. I added a more flexible regex for this issue:

1 Like

OMG thanks. That made so much more sense.

You made it straight!. Thanks!

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

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

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

I still do not pass restructuring with reassignment. Please I need a hand

You renamed the avgTemperatures parameter, which causes the test to fail, since it’s looking for a parameter with exactly that name. Change the name back and your test should be fine.

In the future, don’t edit code above the // change code below this line comment.

I don’t think it is appropriate to give solutions here Kamal. It’s best for the OP to figure this out by themselves, with your guidance.

No idea why his answer (original poster) is wrong. Sure,… it’s not the most efficient,. but it works.

I literally copy pasted his code into my console and it works.

If anything the test is wrong…
On the right side it states:

// running tests

destructuring with reassignment was used

// tests completed

But on left side it says:

(check) `getTempOfTmrw(AVG_TEMPERATURES)` should be  `79`

(x) destructuring with reassignment was used

How confusing is that?

The best way to implement this isn’t even listed…

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

function getTempOfTmrw({tomorrow}) {
    return tomorrow;
}
console.log(getTempOfTmrw(AVG_TEMPERATURES)); // should be 79

Because there is no way to check if destructuring was used other than checking what is written. To avoid a spaghetti test, you can pass only if you implement destructuring in the accepted way

Thanks! Great information.