Skip to content
Open
Show file tree
Hide file tree
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
16 changes: 8 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

150 changes: 138 additions & 12 deletions robbery.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Сделано задание на звездочку
* Реализовано оба метода и tryLater
*/
exports.isStar = true;
exports.isStar = false;

/**
* @param {Object} schedule – Расписание Банды
Expand All @@ -14,8 +14,24 @@ exports.isStar = true;
* @param {String} workingHours.to – Время закрытия, например, "18:00+5"
* @returns {Object}
*/
var GTM_BANK = 0;
var WEEK_DAYS = ['ПН', 'ВТ', 'СР'];
var MINUTES_IN_HOUR = 60;
var HOUR_IN_DAY = 24;
var TOTAL_TIME_LINE = WEEK_DAYS.length * HOUR_IN_DAY * MINUTES_IN_HOUR;
// var TRY_LATER_MINUTES = 30;

Choose a reason for hiding this comment

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

Такого рода комментарии лучше удалять

var DURATION = 0;


exports.getAppropriateMoment = function (schedule, duration, workingHours) {
console.info(schedule, duration, workingHours);
GTM_BANK = workingHours.from.split('+')[1];
DURATION = duration;

let gangPartyFreeTime = calcGangPartyFreeTime(schedule);
let bankFreeTime = getBankWorkingTime(workingHours);
gangPartyFreeTime.push(bankFreeTime);
let timeToGang = findTimeToGang(gangPartyFreeTime);
var currentGang = 0;

return {

Expand All @@ -24,7 +40,7 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) {
* @returns {Boolean}
*/
exists: function () {
return false;
return Boolean(timeToGang[currentGang]);
},

/**
Expand All @@ -35,16 +51,126 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) {
* @returns {String}
*/
format: function (template) {
return template;
},
if (!timeToGang[currentGang]) {
return '';
}
let minutes = timeToGang[currentGang][0];
let day = WEEK_DAYS[(minutes - minutes % (HOUR_IN_DAY * MINUTES_IN_HOUR)) /
(HOUR_IN_DAY * MINUTES_IN_HOUR)];
minutes %= HOUR_IN_DAY * MINUTES_IN_HOUR;
let hours = ((minutes - minutes % MINUTES_IN_HOUR) / MINUTES_IN_HOUR).toString();
minutes %= MINUTES_IN_HOUR;

/**
* Попробовать найти часы для ограбления позже [*]
* @star
* @returns {Boolean}
*/
tryLater: function () {
return false;
return template.replace('%DD', day)
.replace('%HH', (hours < 10 ? '0' : '') + hours)
.replace('%MM', (minutes < 10 ? '0' : '') + minutes);
}

// /**

Choose a reason for hiding this comment

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

???

// * Попробовать найти часы для ограбления позже [*]
// * @star
// * @returns {Boolean}
// */
// tryLater: function () {
// var newTimeStart = timeToGang[currentGang][0] + TRY_LATER_MINUTES;
// if ((timeToGang[currentGang][1] - newTimeStart) < duration) {
// if (!timeToGang[currentGang + 1]) {
// return false;
// }
// currentGang++;
//
// return true;
// }
// timeToGang[currentGang][0] += TRY_LATER_MINUTES;
//
// return true;
// }
};
};

function calcGangPartyFreeTime(scheduleArr) {
var result = [];
for (let personName in scheduleArr) {

Choose a reason for hiding this comment

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

forEach?

if (!scheduleArr.hasOwnProperty(personName)) {
continue;
}
var personResult = [];
for (let i = 0; i < scheduleArr[personName].length; i++) {

Choose a reason for hiding this comment

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

Используешь где-то let, но при этом не используешь const для констант

let start = stringToInt(scheduleArr[personName][i].from);
let end = stringToInt(scheduleArr[personName][i].to);
personResult.push([start, end]);
}
result.push(busyTimeToFreeTime(personResult));
}

return result;
}

function stringToInt(time) {

Choose a reason for hiding this comment

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

Кажется, что это неподходящее название для метода, ибо stringToInt я представляю как parseInt(value, radix)

let format = time.match(/([А-Я]{2})\s(\d{2}):(\d{2})\+(\d+)/);
let hours = WEEK_DAYS.indexOf(format[1]) * HOUR_IN_DAY;
let delta = GTM_BANK - Number(format[4]);
hours += Number(format[2]) + delta;

return hours * MINUTES_IN_HOUR + Number(format[3]);
}

function busyTimeToFreeTime(timeArr) {

Choose a reason for hiding this comment

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

Как-то много индексов и каких-то проверок в этом методе, постарайся хотя бы сделать, чтобы было обращение к свойствам, а не индексам, ибо вообще сложно разобраться что к чему здесь

Choose a reason for hiding this comment

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

За 2 минуты так и не смог понять, что делает этот метод

var result = [];
for (let i = 0; i <= timeArr.length; i++) {

Choose a reason for hiding this comment

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

Посмотри в сторону других перебирающих методов: forEach, map, reduce и т. д.

if (i === 0) {

Choose a reason for hiding this comment

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

Почему такое исключение для первой итерации?

result.push([0, timeArr[i][0]]);
continue;
}
let end = timeArr[i] === undefined ? TOTAL_TIME_LINE : timeArr[i][0];

Choose a reason for hiding this comment

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

Почему оно может быть undefined?

result.push([timeArr[i - 1][1], end]);
}

return result;
}

function getBankWorkingTime(timeArr) {
var result = [];
for (let i = 0; i < WEEK_DAYS.length; i++) {

Choose a reason for hiding this comment

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

Только не for)

result.push([stringToInt(WEEK_DAYS[i] + ' ' + timeArr.from),

Choose a reason for hiding this comment

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

Почему бы здесь не использовать интерполяцию строк?

stringToInt(WEEK_DAYS[i] + ' ' + timeArr.to)]);
}

return result;
}

function findTimeToGang(arrays, compare) {
var newArr = [];
var arr1 = compare ? compare : arrays[0];
var arr2 = compare ? arrays[0] : arrays[1];
for (let i = 0; i < arr1.length; i++) {
for (let k = 0; k < arr2.length; k++) {
// if (arr1[i][0] >= arr2[k][1] || arr1[i][1] <= arr2[k][0]) {

Choose a reason for hiding this comment

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

Удалить

// continue;
// }
// var el = [Math.max(arr1[i][0], arr2[k][0]), Math.min(arr1[i][1], arr2[k][1])];
// if ((el[1] - el[0]) >= duration) {
// newArr.push(el);
// }
checkTimeInterval(arr1[i], arr2[k], newArr);
}
}
if (newArr.length && arrays[1]) {
arrays.shift();
newArr = findTimeToGang(arrays, newArr);
}

return newArr;
}

function checkTimeInterval(arr1, arr2, newArr) {
if (arr1[0] >= arr2[1] || arr1[1] <= arr2[0]) {

Choose a reason for hiding this comment

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

Индексы - сложно и непонятно

return;
}
var el = [Math.max(arr1[0], arr2[0]), Math.min(arr1[1], arr2[1])];
if ((el[1] - el[0]) >= DURATION) {
newArr.push(el);
}
}