Hello everyone! I’d like to ask for a help in explaining to me code below?
Here’s the link to kata Training on Unlucky Days | Codewars
function unluckyDays(year){
let result = 0;
for (let month = 0; month < 12; month++) {
let d = new Date(year, month, 13);
if (d.getDay() == 5) {
result ++
}
}
console.log(result)
return result
}
Generally I understood, how the code works. Let me go through the steps. So to make clear parts, which are unclear for me.
- We need to find unlucky days
- First in a code, variable result was created to count unlucky days. Also for the beginning it’s empty
- Then we’ve to loop through months. That’s why it’s set to 12.
- Then here I know how the object new Date works. I don’t understand, why do we need to put 13 in the end here: “let d = new Date(year, month, 13);”
Year will be taken from argument of the function, month will be taken from loop. But why do we need to add 13 there? - Then we are getting a date out of the above written code and comparing it to 5. This part also confuses me a lot. Why do we need to compare it with 5 I thought we need to compare it with 13.
I know that I didn’t understand most important part of the code. But I want to learn it and be able to use it in the future. Thanks a lot for your time and help!