Below is the data structure for storing affairs according to years, months, and days:
let affairs = {
2018: {
11: {
29: ['task111', 'task112', 'task113'],
30: ['task121', 'task122', 'task123'],
},
12: {
30: ['task211', 'task212', 'task213'],
31: ['task221', 'task222', 'task223'],
},
},
2019: {
12: {
29: ['task311', 'task312', 'task313'],
30: ['task321', 'task322', 'task323'],
31: ['task331', 'task332', 'task333'],
}
},
}
Create a function called addAffair that will add a new affair on a specified date.
Here is the incorrect solution:
addAffair(2020, 10, 21, 'affair111')
console.log(affairs)
function addAffair(year, month, day, affair){
if(affairs[year]===undefined){
affairs[year]={}
}
if(affairs[year][month]===undefined){
affairs[month]={}
}
if(affairs[year][month][day]===undefined){
affairs[day]=[]
}
affairs[year][month][day].push(affair)
}
The error displayed in the console is :
TypeError: Cannot read property '21' of undefined at addAffair (/script.js:36:26) at /script.js:24:1
Please assist in identifying the mistake.