0
What makes it suddenly turn to text?
So below you see the exercise has been completed but I don't get the exact proces behind it. Why does putting the d.getDay in those arrays at the bottom suddenly make it turn into the right text? 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()]; }
1 Odpowiedź
0
var d = new Date(year, month, day);
creates a date object with the current date.
using d you can access the methods of Date(). to do so you use the object.method() form. d in this case is the object.
d.getDay() returns the current day as an int from 0 to 6. Sunday = 0, Monday = 1,..., Saturday =6.
names[d.getDay()] is equivalent to names[0]. as i'm writing this on Sunday. on Monday it will be equivalent to names[1]. and so on.