0

Relatively new to javascript. Just cannot seem to find out why this code doesn't work.

This is part of the Odin Project's javascript exercises, exercises 12: https://github.com/TheOdinProject/javascript-exercises/tree/main/12_findTheOldest

Currently just working on the first part of the tests for this exercise. After returning the variable, it returns with "undefined" and fails the test.

Here is my code. I know it's most likely far from efficient but I'd like to know why this specific way, even if not ideal, doesn't work when tested in the terminal (unless this method just doesn't work at all in this specific circumstance and test). While returning the value returns "undefined", returning it through console.log displays that the variable does have the correct answer stored.

const findTheOldest = function(people) {
    withAges = people.map(x => ({
        ...x, age: Number(x.yearOfDeath) - Number(x.yearOfBirth)
      }));
    let max = Math.max(...withAges.map(x => { return x.age; }));
    let maxFind = withAges.find((x => { return Number(x.age) == Number(max); }));
    let maxName = maxFind.name;
    return maxName;
    //return console.log(maxName) returns the correct answer which is the name Ray.
};

module.exports = findTheOldest;

Here is the spec.js code that tests the result of my code and returns undefined.

const findTheOldest = require('./findTheOldest')

describe('findTheOldest', () => {
  test('finds the oldest person!', () => {
    const people = [
      {
        name: "Carly",
        yearOfBirth: 1942,
        yearOfDeath: 1970,
      },
      {
        name: "Ray",
        yearOfBirth: 1962,
        yearOfDeath: 2011,
      },
      {
        name: "Jane",
        yearOfBirth: 1912,
        yearOfDeath: 1941,
      },
    ]
    expect(findTheOldest(people).name).toBe('Ray');
  });
});
  • 1
    Are you able to create a [mre] of your issue using a [code snippet](https://meta.stackoverflow.com/questions/358992/ive-been-told-to-create-a-runnable-example-with-stack-snippets-how-do-i-do), testing your code, it doesn't return `undefined` like you're saying. Maybe you're experiencing this issue?: [Chrome/Firefox console.log always appends a line saying 'undefined'](https://stackoverflow.com/q/14633968) – Nick Parsons Jan 22 '23 at 11:09
  • 1
    Having checked the instructions on the external site you link to, I think you've just misunderstood this part: `You should return the whole person object, but the tests mostly just check to make sure the name is correct.`. Your code is only returning the `name` property. If you `return maxFind` instead of the last 2 lines it should work. – Robin Zigmond Jan 22 '23 at 11:41
  • You were right, I did misunderstand it. That did the trick, thank you very much @RobinZigmond – payabaheer Jan 22 '23 at 12:04

1 Answers1

0

You return the name instead of the object.

You are actually reading 'Ray'.name which is undefined.

Try remove the .name and you will have it.

U Rogel
  • 1,893
  • 15
  • 30