вот ссылка на тест:
вопрос о тестовом куполеИ вот мое решение, ты можете проверить это напрямую и увидеть результат
Код: Выделить всё
function canTravelTo(gameMatrix, fromRow, fromColumn, toRow, toColumn) {
// Then check bounds
if (
fromRow < 0 ||
fromColumn < 0 ||
toRow < 0 ||
toColumn < 0 ||
fromRow >= gameMatrix.length ||
fromColumn >= gameMatrix[0].length ||
toRow >= gameMatrix.length ||
toColumn >= gameMatrix[0].length
) {
return false;
}
// check if they start or end with false (land)
else if (
gameMatrix[fromRow][fromColumn] === false ||
gameMatrix[toRow][toColumn] === false
) {
return false;
}
// check horizontally
else if (fromRow === toRow) {
const specificArray = gameMatrix[fromRow].slice(fromColumn, toColumn + 1);
const row = specificArray;
row.pop();
row.shift();
if (row.includes(false)) {
console.log("boat can't move horizontally and faced some obstacles");
return false;
}
console.log("boat moved horizontally without obstacles");
}
// check vertically
else if (fromColumn === toColumn) {
const mergedArray = [];
for (let i = fromRow + 1; i < toRow; i++) {
mergedArray.push(gameMatrix[i]);
}
const result = mergedArray.some((row) => row[fromColumn] === false);
if (result) {
console.log("boat can't move vertically and faced some obstacles");
return false;
}
console.log("boat moved vertically without obstacles");
return true;
} else {
return true;
}
return true;
}
Подробнее здесь: https://stackoverflow.com/questions/793 ... rect-answe