Last active
June 28, 2018 17:08
-
-
Save akabab/371a317c1bfe8f038576c1a36657aa34 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const openingHours = { | |
monday: [ | |
{ begin: '10:00', end: '12:00' }, | |
{ begin: '13:00', end: '18:00' }, | |
], | |
tuesday: [ | |
{ begin: '10:00', end: '12:00' }, | |
{ begin: '13:00', end: '18:00' }, | |
], | |
wednesday: [ | |
{ begin: '10:00', end: '12:00' }, | |
{ begin: '13:00', end: '18:00' }, | |
], | |
thursday: [ | |
{ begin: '10:00', end: '12:00' }, | |
{ begin: '13:00', end: '18:00' }, | |
], | |
friday: [], | |
saturday: [], | |
sunday: [], | |
} | |
const weekDays = [ 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday' ] | |
const toMinutes = h => { | |
const [ hours, minutes ] = h.split(':').map(Number) | |
return hours * 60 + minutes | |
} | |
console.log(toMinutes('00:23')) // 23 | |
console.log(toMinutes('01:23')) // 83 | |
console.log(toMinutes('02:00')) // 120 | |
const addHours = (newHours, toDays) => { | |
const days = Object.keys(daysSelected) | |
.filter(key => daysSelected[key] === true) | |
// Improve2. check for collision | |
for (const day of days) { | |
const dayHours = openingHours[day] | |
for (hours of dayHours) { | |
const b1 = toMinutes(newHours.begin) | |
const b2 = toMinutes(hours.begin) | |
const e1 = toMinutes(newHours.end) | |
const e2 = toMinutes(hours.end) | |
// if collides -> return | |
if ((b1 >= b2 && b1 < e2) || (e1 > b2 && e1 <= e2)) { | |
console.error(newHours, 'collides with', hours) | |
return | |
} | |
} | |
} | |
// add | |
for (const day of days) { | |
const dayHours = openingHours[day] | |
dayHours.push(newHours) | |
// Improve1. sort | |
dayHours.sort((a, b) => b.begin < a.begin) | |
} | |
} | |
// Tests | |
const daysSelected = { // should be called 'selectedDays' | |
monday: true, | |
tuesday: true, | |
wednesday: true, | |
thursday: true, | |
friday: false, | |
saturday: false, | |
sunday: false, | |
} | |
console.log(openingHours) | |
addHours({ begin: '06:00', end: '09:00' }, daysSelected) | |
addHours({ begin: '05:00', end: '06:00' }, daysSelected) | |
addHours({ begin: '11:00', end: '13:00' }, daysSelected) // collides | |
addHours({ begin: '07:00', end: '08:00' }, daysSelected) // collides | |
addHours({ begin: '18:00', end: '20:00' }, daysSelected) | |
addHours({ begin: '17:00', end: '20:00' }, daysSelected) // collides | |
addHours({ begin: '22:00', end: '02:00' }, daysSelected) | |
console.log(openingHours) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const cubes = n => n**3 - (n-2)**3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const print = (n = 1, count = 1) => { | |
// limit | |
if (count > 10) return | |
let i = 0 | |
const a = [] | |
while (i < count) { | |
a.push(n + i) | |
i++ | |
} | |
console.log(a.join(' ')) | |
print(n + i, count + 1) | |
} | |
print() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment