+ 1

How Do I Solve This JavaScript Problem?

I am not able to solve this JavaScript code coach problem based on the date object. Can Anyone help me out how to solve this problem?? Please help This is the problem: 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. Code Given by default: 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 }

20th Jan 2021, 7:26 AM
Aditya
Aditya - avatar
7 Answers
+ 15
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); return names[d.getDay()] }
2nd Mar 2022, 2:56 PM
SAMANDAR SHERMATOV
+ 8
function getWeekDay(year, month, day) { var names = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]; //complete the function var d = new Date(year, month, day); return names[d.getDay()] }
17th May 2021, 11:50 PM
Rahul Tiwari
+ 4
Certainly! You can complete the getWeekDay function by using the getDay() method of the Date object to get the index representing the day of the week. Then, you can use this index to access the corresponding day name from the names array. Here's the completed code: javascript Copy code 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 - 1, day); // Note: Months are 0-based in JavaScript Date object (0 = January, 1 = February, ..., 11 = December) var dayIndex = d.getDay(); var dayName = names[dayIndex]; return dayName; } In this code: d = new Date(year, month - 1, day);: Note that months are 0-based in the JavaScript Date object, so we subtract 1 from the month parameter when creating the Date object. var dayIndex = d.getDay();: This gets the day of the week index (0 for Sunday, 1 for Monday, ..., 6 for Saturday). var dayName = names[dayIndex];: This retrieves the corresponding day name from the names array using the obtained day index. Now, the getWeekDay function should return the correct day of the week based on the provided year, month, and day parameters.
20th Dec 2023, 1:00 AM
Joel Ende Nathaniel
Joel Ende Nathaniel - avatar
+ 2
@Rahul Tiwari So, it is short, simple, and sweet solution! :-)
4th Jun 2021, 12:54 PM
Rafique
Rafique - avatar
+ 2
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"]; //complete the function var d = new Date(year, month, day); return names[d.getDay()] } Good Luck
25th Jan 2022, 8:39 AM
Muhammad Alif Deva Rizqon
Muhammad Alif Deva Rizqon - avatar
+ 1
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); return names[d.getDay()] }
12th Mar 2023, 4:30 AM
Shahanshah Hayat
Shahanshah Hayat - avatar