我正在参加我参加的前端开发者训练营的考试。
从考试开始,问题就说明了,
/ *问题03
实现一个函数“checkOverlap”,当给出两个表示线条的对象时, 返回它们是否重叠。
行以这种方式表示:{start:0,end:10}
(结束将始终大于开始)
Example 1:
checkOverlap({start: 0, end: 10}, {start: 8, end: 15})
Which visually, would be:
0--------10
8-------15
^^^^ overlap
Returns:
true
Example 2:
checkOverlap({start: 12, end: 15}, {start: 0, end: 10})
Which visually, would be:
12-------15
0--------10
no overlap
Returns:
false
我为该函数编写的代码是,
function checkOverlap(lineA, lineB) {
/* IMPLEMENT ME */
let aLineArr = [];
let bLineArr = [];
for (let ind = lineA.start; ind < lineA.end + 1; ind++) {
aLineArr.push(ind);
}
for (let ind = lineB.start; ind < lineB.end + 1; ind++) {
bLineArr.push(ind);
}
// Code below passes all ovelapping cases
let overlap = false;
for (var i = 0; i < bLineArr.length; i++) {
if (aLineArr.indexOf(bLineArr[i]) >= 0) {
overlap = true;
return true;
}
}
return overlap;
}
我必须在测试中遇到的条件在以下测试中定义,
const chai = require("chai");
const sinon = require("sinon");
const assert = chai.assert;
const checkOverlap = require("../answers/03.js");
describe("checkOverlap", () => {
describe("overlapping", () => {
it("Sample 1", () => {
var lineA = {start: 0, end: 10};
var lineB = {start: 8, end: 15};
assert.equal(checkOverlap(lineA, lineB), true);
});
it("Sample 1 (Reversed)", () => {
var lineA = {start: 8, end: 15};
var lineB = {start: 0, end: 10};
assert.equal(checkOverlap(lineA, lineB), true);
});
it("One Inside the Other", () => {
var lineA = {start: 0, end: 10};
var lineB = {start: 3, end: 9};
assert.equal(checkOverlap(lineA, lineB), true);
});
it("One Inside the Other (Reversed)", () => {
var lineA = {start: 3, end: 9};
var lineB = {start: 0, end: 10};
assert.equal(checkOverlap(lineA, lineB), true);
});
it("Identical", () => {
var lineA = {start: -10, end: 10};
var lineB = {start: -10, end: 10};
assert.equal(checkOverlap(lineA, lineB), true);
});
});
describe("not overlapping", () => {
it("Sample 2", () => {
var lineA = {start: 12, end: 15};
var lineB = {start: 0, end: 10};
assert.equal(checkOverlap(lineA, lineB), false);
});
it("Sample 2 (Reversed)", () => {
var lineA = {start: 0, end: 10};
var lineB = {start: 12, end: 15};
assert.equal(checkOverlap(lineA, lineB), false);
});
it("Ends Touching", () => {
var lineA = {start: 0, end: 10};
var lineB = {start: 10, end: 20};
assert.equal(checkOverlap(lineA, lineB), false);
});
it("Ends Touching (Reversed)", () => {
var lineA = {start: 10, end: 20};
var lineB = {start: 0, end: 10};
assert.equal(checkOverlap(lineA, lineB), false);
});
it("Far Away", () => {
var lineA = {start: 0, end: 10};
var lineB = {start: 100, end: 200};
assert.equal(checkOverlap(lineA, lineB), false);
});
it("Far Away (Reversed)", () => {
var lineA = {start: 100, end: 200};
var lineB = {start: 0, end: 10};
assert.equal(checkOverlap(lineA, lineB), false);
});
});
});
目前,我已经通过了所有测试,除了两个非重叠的情况(1。当结束触摸时,2。当结束触摸(反转)时......
我想要解决这两个非重叠案例的一些提示/建议!
一些帮助将不胜感激! :)
答案 0 :(得分:4)
为什么要创建新数组?有更简单的解决方案:
function checkOverlap(lineA, lineB) {
return (lineA.start >= lineB.start && lineA.start < lineB.end) || (lineB.start >= lineA.start && lineB.start < lineA.end);
}
编辑:
根据评论中的建议,它可以更简单:
function checkOverlap(lineA, lineB) {
return (lineA.start < lineB.end) && (lineB.start < lineA.end);
}