Skip to content

Игнатенко Алексей #74

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 94 additions & 3 deletions robbery.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,63 @@
* Сделано задание на звездочку
* Реализовано оба метода и tryLater
*/
const isStar = true;
const isStar = false;

const minutesPerHour = 60;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const MINUTES_PER_HOUR = 60;

const hourPerDay = 24;

let weekDays = ['ПН', 'ВТ', 'СР', 'ЧТ', 'ПТ', 'СБ', 'ВС'];

let dayTimeStrToMinutes = (weekDay, timeStr, timeZone, bankTimeZone) =>
[weekDays.indexOf(weekDay) * minutesPerHour * hourPerDay,
timeStr.split(':')[0] * minutesPerHour + Number(timeStr.split(':')[1]),
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

timeStr.split(':') можно в переменную, чтобы 2 раза не сплитить

(bankTimeZone - (timeZone === undefined ? 0 : Number(timeZone))) * minutesPerHour
].reduce((a, b) => a + b);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

какой тут смысл от reduce, когда можно просто сложить переменные?


let addTimeToTwoChars = (time) => time.toString().length === 2 ? time.toString() : `0${time}`;

function minutesToDayTime(minutes, bankTimeZone) {
let weekDay = parseInt(minutes / (minutesPerHour * hourPerDay));
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

parseInt(minutes / (minutesPerHour * hourPerDay), 10);

minutes -= minutesPerHour * hourPerDay * parseInt(minutes / (minutesPerHour * hourPerDay));
weekDay = weekDays[weekDay];
let hours = parseInt(minutes / minutesPerHour);
minutes -= hours * minutesPerHour;
bankTimeZone = !bankTimeZone ? '' : `+${bankTimeZone}`;

return {
day: weekDay,
hours: addTimeToTwoChars(hours),
minutes: addTimeToTwoChars(minutes),
tz: bankTimeZone };
}

function getBankSchedule(workingHours, bankTimeZone) {
let endTime = dayTimeStrToMinutes(...`СР 23:59+${bankTimeZone}`.split(/ |\+/), bankTimeZone);
let endWeek = dayTimeStrToMinutes(...`ВС 23:59+${bankTimeZone}`.split(/ |\+/), bankTimeZone);

return weekDays
.slice(0, 3)
.map((day)=>
-dayTimeStrToMinutes(...`${day} ${workingHours.from}`.split(/ |\+/), bankTimeZone))
.concat(
weekDays
.slice(0, 3)
.map((day)=>
dayTimeStrToMinutes(
...`${day} ${workingHours.to}`.split(/ |\+/), bankTimeZone)))
.concat([endTime, -endWeek]);
}

function getSign(number) {
let sign = isNaN(Math.sign(number)) ? 1 : Math.sign(number);
if (isNaN(sign)) {
sign = 1;
} else if (sign === 0) {
sign = -1;
}

return sign;
}

/**
* @param {Object} schedule – Расписание Банды
Expand All @@ -16,6 +72,37 @@ const isStar = true;
*/
function getAppropriateMoment(schedule, duration, workingHours) {
console.info(schedule, duration, workingHours);
let bankTimeZone = Number(workingHours.from.split('+')[1]);
bankTimeZone = isNaN(bankTimeZone) ? 0 : bankTimeZone;
workingHours = getBankSchedule(workingHours, bankTimeZone);
let answer = [NaN].concat(Object.keys(schedule)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

такой код очень сложно читать и понимать
что тут вообще происходит? напиши комментарии, на какой строчке что делается

Например, зачем [NaN] в начале?
Где заканчивается вызов [NaN].concat(... ?

.map((friend)=>[...schedule[friend]])
.reduce((a, b)=>a.concat(b), [])
.map((record)=>
[
dayTimeStrToMinutes(...record.from.split(/ |\+/), bankTimeZone),
-dayTimeStrToMinutes(...record.to.split(/ |\+/), bankTimeZone)])
.reduce((a, b)=>a.concat(b), [])
.concat(workingHours))
.sort((a, b) => Math.abs(a) - Math.abs(b))
.reduce(function (result, time) {
let lastInterval = result.intervals[result.intervals.length - 1];
let sign = getSign(time);
if (!(result.count += sign)) {
result.intervals.push({ from: -time, to: 0 });
} else if (result.count === 1 && (lastInterval !== undefined) && !(lastInterval.to)) {
result.intervals[result.intervals.length - 1].to = time;
}

return result;
}, { count: 0, intervals: [] })
.intervals
.filter((record)=>Math.abs(record.to - record.from) >= duration)
.map((record)=> {
return {
from: minutesToDayTime(record.from, bankTimeZone),
to: minutesToDayTime(record.to, bankTimeZone) };
});

return {

Expand All @@ -24,7 +111,7 @@ function getAppropriateMoment(schedule, duration, workingHours) {
* @returns {Boolean}
*/
exists: function () {
return false;
return Boolean(answer.length);
},

/**
Expand All @@ -34,7 +121,11 @@ function getAppropriateMoment(schedule, duration, workingHours) {
* @returns {String}
*/
format: function (template) {
return template;
return !answer.length
? '' : template
.replace('%DD', answer[0].from.day)
.replace('%HH', answer[0].from.hours)
.replace('%MM', answer[0].from.minutes);
},

/**
Expand Down