Skip to main content

Ivan Teoh

Something personal yet public

167-exercise49.js

(Source)

function oldestCat(cats) {
    var cat;
    var oldest;
    for (var name in cats) {
        if (!("death" in cats[name]) && !oldest || (oldest > cats[name].birth))
        {
            oldest = cats[name].birth;
            cat = cats[name].name;
        }
    }
  return cat;
}
var cats = {"Spot": {name:"Spot", birth: new Date(1997, 2, 5),
            mother: "Yellow"},
            "White": {name:"White", birth: new Date(1998, 7, 4),
            mother: "Zebra"},
            "Grey": {name:"Grey", birth: new Date(1996, 6, 3),
            mother: "Kitten"}};
document.write(oldestCat(cats)); // "Grey"