코딩테스트(JavaScript)/코딩테스트 입문

프로그래머스-배열 비교하기

paintover23 2023. 7. 20. 17:59
728x90

function solution(arr1, arr2) {
    var answer = 0;
    
    if(arr1.length>arr2.length) answer += 1;
    if(arr1.length<arr2.length) answer += -1;
    
    if(arr1.length==arr2.length) {
        var sum1 = 0;
        var sum2 = 0;
        
        for(var i=0; i<arr1.length; i++) {
        sum1 += arr1[i];
        }
        for(var i=0; i<arr2.length; i++) {
        sum2 += arr2[i];
        }
    }
    
    if(sum1 > sum2) return answer +=1;
    if(sum1 < sum2) return answer +=-1;
    if(sum1 === sum2) return answer +=0;
}
728x90
반응형