0
const record = [
    { year: "1980", result: "N/A"},
    { year: "1979", result: "N/A"},
    { year: "1978", result: "N/A"},
    { year: "1977", result: "N/A"},
    { year: "1976", result: "N/A"},
    { year: "1975", result: "N/A"},
    { year: "1974", result: "N/A"},
    { year: "1973", result: "N/A"},
    { year: "1972", result: "N/A"},
    { year: "1971", result: "N/A"},
    { year: "1970", result: "N/A"},
    { year: "1969", result: "W"},
    { year: "1968", result: "N/A"},
    { year: "1967", result: "N/A"},
    { year: "1966", result: "L"},
    { year: "1965", result: "N/A"},
    { year: "1964", result: "N/A"},
    { year: "1963", result: "N/A"},
    { year: "1962", result: "N/A"},
    { year: "1961", result: "N/A"},
    { year: "1960", result: "N/A"}
  ]


const winYear = (elem) => {
    if (elem.result === "W"){ 
        console.log(elem.year)
        return  elem.year
    }
}


const superbowlWin = (arr) => {
    return arr.find(winYear)
}

console.log(superbowlWin(record))

The goal is to return only the year, not the entire object. So, my intention is for 1969 to be returned. I logged elem.year to the console and it logs 1969 however as soon as I try to return the value, I get the entire object.

What am I missing?

  • 2
    `find` returns the array element for which the callback returns true, it does not return the return value of the callback. Use `return arr.find(elem => elem.result == "W")?.year` – Bergi Apr 17 '21 at 03:33
  • That makes sense. The assignment I'm working on requires `find`. It sounds like you're saying this isn't the best way to go about it though? I tried declaring a global variable and storing the returned object into that variable and then retrieved the variable of year which passed the first test but failed the second test. These are the two tests: `1) returns a year the KC Chiefs won the superbowl 2) returns undefined if the record has no win objects` – Travis Courtney Apr 17 '21 at 03:48
  • Also, I tried your solution and it was erroring out because of the `.` before `year` `SyntaxError: Unexpected token '.'` – Travis Courtney Apr 17 '21 at 03:53
  • 1
    ```let winYear = record => record.result === "W" let superbowlWin = (record) => { const result = record.find(winYear) return !!result ? result.year : undefined; } ``` Here's my solution – Travis Courtney Apr 17 '21 at 04:33

0 Answers0