I am trying to write a function that uses find() to return the year of superbowl win ("W"). This is the array object
const record = [
{ year: "2018", result: "N/A"},
{ year: "2017", result: "N/A"},
{ year: "2016", result: "N/A"},
{ year: "2015", result: "W"},
{ year: "2014", result: "N/A"},
{ year: "2013", result: "L"},
{ year: "2012", result: "N/A"},
{ year: "2011", result: "N/A"},
{ year: "2010", result: "N/A"},
{ year: "2009", result: "N/A"},
{ year: "2008", result: "N/A"},
{ year: "2007", result: "N/A"},
{ year: "2006", result: "N/A"},
{ year: "2005", result: "N/A"},
{ year: "2004", result: "N/A"},
{ year: "2003", result: "N/A"},
{ year: "2002", result: "N/A"},
{ year: "2001", result: "N/A"},
{ year: "2000", result: "N/A"},
{ year: "1999", result: "N/A"},
{ year: "1998", result: "W"},
{ year: "1997", result: "W"},
{ year: "1996", result: "N/A"},
{ year: "1995", result: "N/A"},
{ year: "1994", result: "N/A"},
{ year: "1993", result: "N/A"},
{ year: "1992", result: "N/A"},
{ year: "1991", result: "N/A"},
{ year: "1990", result: "N/A"},
{ year: "1989", result: "L"},
{ year: "1988", result: "N/A"},
{ year: "1987", result: "L"},
{ year: "1986", result: "L"},
{ year: "1985", result: "N/A"},
{ year: "1984", result: "N/A"},
{ year: "1983", result: "N/A"},
{ year: "1982", result: "N/A"},
{ year: "1981", result: "N/A"},
{ year: "1980", result: "N/A"},
{ year: "1979", result: "N/A"},
{ year: "1978", result: "N/A"},
{ year: "1977", result: "N/A"},
{ year: "1976", result: "L"},
{ 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: "N/A"},
{ year: "1968", result: "N/A"},
{ year: "1967", result: "N/A"},
{ year: "1966", result: "N/A"},
{ 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"}
]
function superbowlWin (element){
if(element.result === "W"){
const winYear = element.year
return winYear;
}
}
console.log(record.find(superbowlWin).year);`
My tests fail with the error below:
superbowlWin(record) 1) returns a year the Denver Broncos won the superbowl ✓ returns undefined if the record has no win objects
1 passing (377ms) 1 failing
- superbowlWin(record). returns a year the Denver Broncos won the superbowl. AssertionError: expected undefined to equal '2015' at Context. (test/indexTest.js:66:33) at processImmediate (internal/timers.js:461:21)
I was expecting the return winYear to equal 2015.
Your problem is when there is no value for your find request. The return value is undefined then you are trying to get the year of an undefined object.
Try:
function superbowlWin(records){
const winRecord = records.find(element => element.result === "W");
return winRecord ? winRecord.year : undefined;
}
console.log(superbowlWin(record));
The line return winRecord ? winRecord.year : undefined;
checks if the winRecord
has value if yes return the year if not return undefined.