[SOLVED] findTheOldest Exercise Fundamentals 5 help

Hey all!
I’m having trouble creating a function that takes a dictionary object of people, analyzes their age, and returns the oldest person.

I believe the issue I’m having is that my global variable, oldestAge, refuses to change even though within the if statement I have "oldestAge == age";. I think it may be due to the loop/if statement creating a local variable version of “oldestAge” instead of affecting the global variable, but I’m having a hard time finding info on changing variables within a for-in loop. If you run the code, you’ll see that the console.log shows oldestAge continues to equal 0.

Here’s the provided dictionary:
const people = [ { name: 'Carly', yearOfBirth: 1942, yearOfDeath: 1970, }, { name: 'Ray', yearOfBirth: 1962, yearOfDeath: 2011 }, { name: 'Jane', yearOfBirth: 1912, yearOfDeath: 1941 }, ]

Here’s my code:

let findTheOldest = function(dict) {
    let oldestAge = 0;
    let oldestPerson;

    for (let key in dict){
        let age = dict[key].yearOfDeath - dict[key].yearOfBirth;
        console.log('Age is: ' + age);
    
        if (age > oldestAge){
            oldestAge == age;
            console.log('Current oldest age is: ' + oldestAge);
            console.log(dict[key]);
            oldestPerson = dict[key];
            console.log('Oldest Person so far: ' + oldestPerson.name);

    } else {
        console.log('Not older than current oldest age');
    }
  
}
return oldestPerson;

}

Thanks for your time and help!

Yes the problem is this line, this isnt an asignation operation, is a comparation operation.

use instead

 oldestAge = age;

Hope this will help

1 Like

Solved! It’s funny how sometimes your brain just doesn’t register the most obvious mistakes.

Thanks for your time and help!