Can anyone help me on this challenge "Monday to Sunday" JavaScript 😅 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Can anyone help me on this challenge "Monday to Sunday" JavaScript 😅

The Date Object The program you are given takes year, month and day as input. Create a function that takes them as arguments and returns the corresponding day of the week. Sample Input 1993 7 12 Sample Output Thursday Hint: The given code creates a Date object from the parameters. Use the getDay() method of the date object to get the index, then use it in the given names array to return the name of the day. function main() { var year = parseInt(readLine(), 10); var month = parseInt(readLine(), 10); var day = parseInt(readLine(), 10); 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); //complete the function console.log(names[d.getDay()]); } the error message i got is "undefined" Thanks for you help

18th Aug 2021, 11:06 AM
Aadel Boo
Aadel Boo - avatar
5 Answers
+ 6
Where you wrote console.log( names[ d.getDay() ] ); You instead return return names[ d.getDay() ]; You got 'undefined' because getWeekDay() doesn't return anything while something is awaited for in main() to be logged into JS console.
18th Aug 2021, 11:30 AM
Ipang
+ 2
using the get method on the Date attributes (year, month, and day) and returning the name of the day will also answer this question. //complete the function var year = d.getFullYear(); var month = d.getMonth(); var day = d.getDay(); return names[day];
8th Feb 2022, 6:25 AM
ENOCH KWATEH DONGBO
ENOCH KWATEH DONGBO - avatar
+ 1
This small corrections doesn't needed effect)
12th Jan 2022, 6:14 AM
Vladimir Kushner
Vladimir Kushner - avatar
+ 1
Exellent solve ! 🏅
8th Feb 2022, 8:33 PM
Vladimir Kushner
Vladimir Kushner - avatar
0
small corrections can be made be in your first code: function getWeekDay(year, month, day) { var names = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]; var d = new Date(); d.setFullYear(year, month, day); //complete the function document.write(names[d.getDay()]); } getWeekDay(1993,7,12); just copy paste and see :)
12th Sep 2021, 8:01 AM
Ananth Shetty
Ananth Shetty - avatar