-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquestion1232.js
34 lines (34 loc) · 1004 Bytes
/
question1232.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/**
* @param {number[][]} coordinates
* @return {boolean}
*/
const checkStraightLine = function(coordinates) {
if (coordinates.length === 2) {
return true;
}
if (coordinates[0][1] - coordinates[1][1] === 0) {
for (let i = 1; i < coordinates.length - 1; i++) {
if (coordinates[i][1] - coordinates[i + 1][1] !== 0) {
return false;
}
}
return true;
}
let slope =
(coordinates[0][0] - coordinates[1][0]) /
(coordinates[0][1] - coordinates[1][1]);
for (let i = 1; i < coordinates.length - 1; i++) {
if (coordinates[i][1] - coordinates[i + 1][1] === 0) {
return false;
}
if (
(coordinates[i][0] - coordinates[i + 1][0]) /
(coordinates[i][1] - coordinates[i + 1][1]) !==
slope
) {
return false;
}
}
return true;
};
checkStraightLine([[1, 2], [2, 3], [3, 4], [4, 5], [5, 6], [6, 7]]);