두 개의 숫자 배열을 취하는 JavaScript 함수를 작성해야 합니다.
그리고 함수는 결합 및 셔플링 시 두 배열이 연속적인 시퀀스를 형성할 수 있으면 true를 반환해야 하고 그렇지 않으면 false를 반환해야 합니다.
예를 들어 - 배열이 -
인 경우const arr1 = [4, 6, 2, 9, 3]; const arr2 = [1, 5, 8, 7];
그러면 출력이 참이어야 합니다.
예시
다음은 코드입니다 -
const arr2 = [1, 5, 8, 7];
const canFormSequence = (arr1, arr2) => {
const combined = [...arr1, ...arr2];
if(combined.length < 2){
return true;
};
combined.sort((a, b) => a-b);
const commonDifference = combined[0] - combined[1];
for(let i = 1; i < combined.length-1; i++){
if(combined[i] - combined[i+1] === commonDifference){
continue;
};
return false;
};
return true;
};
console.log(canFormSequence(arr1, arr2)); 출력
다음은 콘솔의 출력입니다 -
true