[05.02.22] Coding test

Juyeon.it·2022년 5월 2일
0

Coding test

목록 보기
21/32

Are they the "same"?

Description

comp(a, b) returns true because in b 121 is the square of 11, 14641 is the square of 121, 20736 the square of 144, 361 the square of 19, 25921 the square of 161, and so on. It gets obvious if we write b's elements in terms of squares:

a = [121, 144, 19, 161, 19, 144, 19, 11] 
b = [11*11, 121*121, 144*144, 19*19, 161*161, 19*19, 144*144, 19*19]

My answer

function comp(array1, array2){
  if (!array1 || !array2) { return false };
  
  let sortedArray1 = array1.sort(function(a,b) { return a-b });
  let sortedArray2 = array2.sort(function(a,b) { return a-b });
  
  let calculatedArray1 = sortedArray1.map(a => a*a)
  for(let i = 0; i < calculatedArray1.length; i++) {
    if (calculatedArray1[i] !== sortedArray2[i]) {
      return false;
    }
  }
  return true;
}

0개의 댓글

관련 채용 정보