Always read and reread the requirements

So I spend some 10 hours doing the Exercise Tracker project. About 7 hours were spent just testing to see if things would work and debugging.
Tbh if I had read some requirements with more care it would have taken at least 2 hours less debugging time.

Now at the end when I tested everything on postman and my localhost I decided to give it a go on fcc and I was failing ~50% of the test.

Moral of the story: check your routes from time to time.

This:

router.post('/:_id/exercises', createNewExercise);
router.get('/:_id/logs', getExerciseForUser);

is not this:

router.post('/:_id/exercises', createNewExercise);
router.get('/:_id/logs', getExerciseForUser);

Somehow I managed to get it working and by that, I mean that I just used the right path. The code is awful, might post it after some refactoring. There were a lot of things to learn. Thanks, fCC!
Also:

Just noticed that filter's return doesn’t change the array you call it on but map might with the callback. Still investigating. In that specific situation using a for loop didn’t seem to be an option.

4 Likes

Can you show us an example in which map changes the array you call it on?

Hei, of course:

        answer = {
            "_id": answer._id,
            "username": answer.username,
            "count": answer.count,
            "log":
                answer.log.map((element) => {
                    return {
                        "description": element.description,
                        "duration": element.duration,
                        "date": new Date(element.date).toDateString(),
                    }
                })
        }

Here I’m talking about the date element. Changing map with filter will give 2 different results as filter won’t apply the new Date(element.date).toDateString()

At this point, answer is an object and has the same structure that I’m rebuilding. I’m just changing its date type from date to string.

Just finished the last Back End Development and APIs Project: File Metadata Microservice.

Was it supposed to be like a small reward for completing the others? As long as I’m not missing something it’s way easier than the 2 projects before it. You don’t even have to write files or use a db. Quite strange. I thought that the projects were placed in increasing difficulty.

Maybe it don’t fully understand your example:

var answer = {
  log: [{ date: "1" }, { date: "2" }],
};
console.log("original:", answer.log);
// original: [ { date: '1' }, { date: '2' } ]

var answer2 = {
  _id: answer._id,
  username: answer.username,
  count: answer.count,
  log: answer.log.map((element) => {
    return {
      date: new Date(element.date).toDateString(),
    };
  }),
};
console.log("original:", answer.log);
// original: [ { date: '1' }, { date: '2' } ]

console.log("new:", answer2.log);
// new: [ { date: 'Mon Jan 01 2001' }, { date: 'Thu Feb 01 2001' } ]

So in my example the original array doesn’t get changed by map.

Yea my bad. I’m still learning to formulate my ideas clearly. For me this difference between filter and map was strange:

module.exports.mapMutation = function mapMutation() {
    var answer = {
        log: [{ date: "2022-02-03T22:35:26.886+00:00" }, { date: "1998-02-03T22:35:26.886+00:00" }],
      };
    //   console.log("original:", answer.log);
      // original: [ { date: '1' }, { date: '2' } ]
      
      var answer2 = {
        log: answer.log.map((element) => {
          return {
            date: new Date(element.date).toDateString(),
          };
        }),
      };

      var answer3 = {
        log: answer.log.filter((element) => {
          return {
            date: new Date(element.date).toDateString(),
          };
        }),
      };
      console.log("original:", answer.log);
    //   original: [
    //     { date: '2022-02-03T22:35:26.886+00:00' },
    //     { date: '1998-02-03T22:35:26.886+00:00' }
    //   ]

      console.log("new with map:", answer2.log);
    //   new with map: [ { date: 'Fri Feb 04 2022' }, { date: 'Wed Feb 04 1998' } ]

      console.log("new with filter:", answer3.log);
    //   new with filter: [
    //     { date: '2022-02-03T22:35:26.886+00:00' },
    //     { date: '1998-02-03T22:35:26.886+00:00' }
}

I meant to say that by using map I could attribute to let’s say answer2 a modified value from answer. With filter I expected it to work the same but that wasn’t the case.

1 Like

@miku86 is I guess this was to be expected as per MDN:
map:

Return value:
A new array with each element being the result of the callback function.

and filter:

Return value:
A new array with the elements that pass the test. If no elements pass the test, an empty array will be returned.

My efforts to change the elements was in vain with filter :sweat_smile:

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.