0

what is the reason for the output of undefined

the exercise is to make the exit the day of the week I get that output but it adds undefined I don't know why it adds undefined, what is the reason? This is the code: var year = 2020; var month = 5; var day = 12; console.log(getWeekDay(year, month, day)); function getWeekDay(year, month, day) { var names = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]; var d = new Date(year, month, day); //completa la funciĂłn console.log(names[d.getDay()]) }

8th Apr 2021, 4:09 PM
Richard
2 Answers
+ 6
getWeekDay doesn't have a return value. So when you say console.log(getWeekDay(...)) it logs undefined, since there was no return value from the function. Either change console.log(names[...]) to return names[...] or just call getWeekDay(...) from main(), not console.log(getWeekDay(...))
8th Apr 2021, 4:18 PM
Russ
Russ - avatar
+ 1
Thanks
8th Apr 2021, 4:29 PM
Richard