what is the reason for the output of undefined | Sololearn: Learn to code for FREE!

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
2 ответов
+ 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 - avatar
+ 1
8th Apr 2021, 4:29 PM