Description:
You are given an array of dates. Your task is to create an array of arrays where each sub array is all the dates contained in that week. A week starts on Monday, and ends on Sunday.
Example Input:
[
"3/24/2021",
"3/25/2021",
"3/1/2021",
"3/6/2021",
"3/10/2021"
]
Expected Output
[
["3/1/2021", "3/6/2021"],
["3/10/2021"],
["3/24/2021", "3/25/2021"],
]
function groupByWeek(days) { const dates = days.map(a => new Date(a)).sort((a, b) => b - a); let currentMonday = new Date(); currentMonday.setDate(dates[0].getDate() - dates[0].getDay()) let currentSunday = new Date() currentSunday.setDate(currentMonday.getDate() + 6); let weeks = [] let currentWeek = [] for(let date of dates) { if(date >= currentMonday && date <= currentSunday) { currentWeek.push((date.getMonth()+1) + "/" + date.getDate() + "/" + date.getFullYear()) } else { if(currentWeek.length > 0) { weeks.push(currentWeek) } currentWeek = [(date.getMonth()+1) + "/" + date.getDate() + "/" + date.getFullYear()] currentMonday.setDate(date.getDate() - date.getDay()) currentSunday.setDate(currentMonday.getDate() + 6); } } if(currentWeek.length > 0) { weeks.push(currentWeek) } return weeks }
- function groupByWeek(days) {
var dates = days.map(a => new Date(a)).sort((a, b) => b - a);console.log(dates.map(a => (a.getMonth()+1) + "/" + a.getDate() + "/" + a.getFullYear()))- const dates = days.map(a => new Date(a)).sort((a, b) => b - a);
var currentMonday = new Date();- let currentMonday = new Date();
- currentMonday.setDate(dates[0].getDate() - dates[0].getDay())
var currentSunday = new Date()- let currentSunday = new Date()
- currentSunday.setDate(currentMonday.getDate() + 6);
//console.log(firstMonday.getDay())//console.log(firstSunday.getDay())var weeks = []var currentWeek = []for(var date of dates) {- let weeks = []
- let currentWeek = []
- for(let date of dates) {
- if(date >= currentMonday && date <= currentSunday) {
- currentWeek.push((date.getMonth()+1) + "/" + date.getDate() + "/" + date.getFullYear())
- } else {
- if(currentWeek.length > 0) {
- weeks.push(currentWeek)
- }
- currentWeek = [(date.getMonth()+1) + "/" + date.getDate() + "/" + date.getFullYear()]
- currentMonday.setDate(date.getDate() - date.getDay())
- currentSunday.setDate(currentMonday.getDate() + 6);
- }
- }
- if(currentWeek.length > 0) {
- weeks.push(currentWeek)
- }
- return weeks
- }
// Since Node 10, we're using Mocha. // You can use `chai` for assertions. const chai = require("chai"); const assert = chai.assert; // Uncomment the following line to disable truncating failure messages for deep equals, do: // chai.config.truncateThreshold = 0; // Since Node 12, we no longer include assertions from our deprecated custom test framework by default. // Uncomment the following to use the old assertions: // const Test = require("@codewars/test-compat"); describe("Solution", function() { it("test case 1", function() { assert.deepEqual( groupByWeek([ "11/25/2021", "3/25/2021", "3/1/2021", "3/6/2021", "5/6/2021", "3/10/2021" ]), [ [ '11/25/2021' ], [ '5/6/2021' ], [ '3/25/2021' ], [ '3/10/2021' ], [ '3/6/2021', '3/1/2021' ] ] ); }); it("test case 2", function() { assert.deepEqual( groupByWeek([ "3/24/2021", "3/25/2021", "3/1/2021", "3/6/2021", "3/10/2021" ]), [ ["3/25/2021", "3/24/2021"], ["3/10/2021"], ["3/6/2021", "3/1/2021"], ] ); }) });
- // Since Node 10, we're using Mocha.
- // You can use `chai` for assertions.
- const chai = require("chai");
- const assert = chai.assert;
- // Uncomment the following line to disable truncating failure messages for deep equals, do:
- // chai.config.truncateThreshold = 0;
- // Since Node 12, we no longer include assertions from our deprecated custom test framework by default.
- // Uncomment the following to use the old assertions:
- // const Test = require("@codewars/test-compat");
- describe("Solution", function() {
- it("test case 1", function() {
- assert.deepEqual(
- groupByWeek([
- "11/25/2021",
- "3/25/2021",
- "3/1/2021",
- "3/6/2021",
- "5/6/2021",
- "3/10/2021"
- ]),
- [
- [ '11/25/2021' ],
- [ '5/6/2021' ],
- [ '3/25/2021' ],
- [ '3/10/2021' ],
- [ '3/6/2021', '3/1/2021' ]
- ]
- );
- });
- it("test case 2", function() {
- assert.deepEqual(
- groupByWeek([
- "3/24/2021",
- "3/25/2021",
- "3/1/2021",
- "3/6/2021",
- "3/10/2021"
- ]),
- [
- ["3/25/2021", "3/24/2021"],
- ["3/10/2021"],
- ["3/6/2021", "3/1/2021"],
- ]
- );
- })
- });
You are given an array of dates. Your task is group the dates into seperate weeks. The week starts on Monday, and ends on Sunday.
example input
[
3/24/2021,
3/25/2021,
3/1/2021,
3/6/2021,
3/10/2021
]
expected output
[
[3/1/2021, 3/6/2021],
[3/10/2021],
[3/24/2021, 3/25/2021],
]
function groupByWeek(days) {
var dates = days.map(a => new Date(a)).sort((a, b) => b - a);
console.log(dates.map(a => (a.getMonth()+1) + "/" + a.getDate() + "/" + a.getFullYear()))
var currentMonday = new Date();
currentMonday.setDate(dates[0].getDate() - dates[0].getDay())
var currentSunday = new Date()
currentSunday.setDate(currentMonday.getDate() + 6);
//console.log(firstMonday.getDay())
//console.log(firstSunday.getDay())
var weeks = []
var currentWeek = []
for(var date of dates) {
if(date >= currentMonday && date <= currentSunday) {
currentWeek.push((date.getMonth()+1) + "/" + date.getDate() + "/" + date.getFullYear())
} else {
if(currentWeek.length > 0) {
weeks.push(currentWeek)
}
currentWeek = [(date.getMonth()+1) + "/" + date.getDate() + "/" + date.getFullYear()]
currentMonday.setDate(date.getDate() - date.getDay())
currentSunday.setDate(currentMonday.getDate() + 6);
}
}
if(currentWeek.length > 0) {
weeks.push(currentWeek)
}
return weeks
}
// Since Node 10, we're using Mocha.
// You can use `chai` for assertions.
const chai = require("chai");
const assert = chai.assert;
// Uncomment the following line to disable truncating failure messages for deep equals, do:
// chai.config.truncateThreshold = 0;
// Since Node 12, we no longer include assertions from our deprecated custom test framework by default.
// Uncomment the following to use the old assertions:
// const Test = require("@codewars/test-compat");
describe("Solution", function() {
it("test case 1", function() {
assert.deepEqual(
groupByWeek([
"11/25/2021",
"3/25/2021",
"3/1/2021",
"3/6/2021",
"5/6/2021",
"3/10/2021"
]),
[
[ '11/25/2021' ],
[ '5/6/2021' ],
[ '3/25/2021' ],
[ '3/10/2021' ],
[ '3/6/2021', '3/1/2021' ]
]
);
});
});