문제(출처: 프로그래머스)

문자열 my_string이 매개변수로 주어집니다. my_string안의 모든 자연수들의 합을 return하도록 solution 함수를 완성해주세요.

 

▶ 내가 푼 방식

//내가 작성한 코드
function solution(my_string) {
    const numbers = my_string.replace(/\D/g, '');
    const numberArray = [...numbers].map(Number)
    const answer = numberArray.reduce((a,b) => a+ b, 0) 
    return answer;
}

 

 다른 유저가 푼 방식

// 유저 1
function solution(my_string) {
    const answer = my_string.replace(/[^0-9]/g, '')
                            .split('')
                            .reduce((acc, curr) => acc + Number(curr), 0);
    return answer;
}

// 유저 2
function solution(my_string) {
    let sum = 0;
    for (const ch of my_string) {
        if (!isNaN(ch)) sum += +ch;
    }
    return sum;
}

 

 배운 것들

     -  숫자로 시작하지 않는  문자를 찾는  정규 표현식 /[^0-9]/g

     - array.reduce((accumulator, currentValue) => 원하는계산, initialValue)

문제(출처: 프로그래머스)

영어에선 a, e, i, o, u 다섯 가지 알파벳을 모음으로 분류합니다. 문자열 my_string이 매개변수로 주어질 때 모음을 제거한 문자열을 return하도록 solution 함수를 완성해주세요.

 

▶ 내가 푼 방식

//내가 작성한 코드
function solution(my_string) {
    const my_arry = [...my_string]
    const vowelList = ['a', 'e', 'i', 'o', 'u']
    const answer = my_arry.filter(w => !vowelList.includes(w))
    return answer.join('')
}

 

 다른 유저가 푼 방식

// 유저 1: 정규표현식을 이용
function solution(my_string) {
    return my_string.replace(/[aeiou]/g, '');
}

 

 배운 것들

     -  문자를 다룰때는 정규표현식을 꼭 생각하자

문제(출처: 프로그래머스)

정수 배열 numbers가 매개변수로 주어집니다. numbers의 원소 중 두 개를 곱해 만들 수 있는 최댓값을 return하도록 solution 함수를 완성해주세요.

 

▶ 내가 푼 방식

//내가 작성한 코드
function solution(numbers) {
    let answer = 0;
    numbers.sort(function(a, b)  {
        if(a > b) return -1;
        if(a === b) return 0;
        if(a < b) return 1;
    });
    return numbers[0]*numbers[1];
}

 

 다른 유저가 푼 방식

// 유저 1  : 다른 정렬방식
function solution(numbers) {
    let [a, b] = numbers.sort((a,b) => b - a);
    return a * b;
}


// 유저 2 : 내가 해보고 싶었던 방식
function solution(numbers) {
    const max1 = Math.max(numbers)
    const numbers = numbers.splice(numbers.indexOf(max1), 1);
    const max2 = Math.max(numbers)

    return max1 * max2;
}

 

 배운 것들

     - 첫번째 유저의 정렬방식

     -  배열에서 특정 요소의 인덱스를 쉽게 차는 방법 : arr.indexOf(value)

문제(출처: 프로그래머스)

머쓱이는 직육면체 모양의 상자를 하나 가지고 있는데 이 상자에 정육면체 모양의 주사위를 최대한 많이 채우고 싶습니다. 상자의 가로, 세로, 높이가 저장되어있는 배열 box와 주사위 모서리의 길이 정수 n이 매개변수로 주어졌을 때, 상자에 들어갈 수 있는 주사위의 최대 개수를 return 하도록 solution 함수를 완성해주세요.

 

▶ 내가 푼 방식

//내가 작성한 코드
function solution(box, n) {
    const boxH = box[2];
    const boxW = box[0];
    const boxL = box[1];
    const numH = Math.floor(boxH/n);
    const numW = Math.floor(boxW/n);
    const numL = Math.floor(boxL/n);
    return (numW*numL)*numH;
}

 

 다른 유저가 푼 방식

// 유저 1
function solution(box, n) {
    let [width, length, height] = box;

    return Math.floor(width / n) * Math.floor(length / n) * Math.floor(height / n);

}

 

 배운 것들

     - 구조 분해 할당을 깜빡했네

     - 

문제(출처: 프로그래머스)

가위는 2 바위는 0 보는 5로 표현합니다. 가위 바위 보를 내는 순서대로 나타낸 문자열 rsp가 매개변수로 주어질 때, rsp에 저장된 가위 바위 보를 모두 이기는 경우를 순서대로 나타낸 문자열을 return하도록 solution 함수를 완성해보세요.

 

▶ 내가 푼 방식

//내가 작성한 코드
function solution(rsp) {
    var answer = '';
    [...rsp].forEach(i=>{
        switch(i){
            case "0":
                answer += "5";
                break;
            case "2":
                answer += "0";
                break;
            case "5":
                answer += "2";
                break;
        }
    })
    return answer;
}

 

 다른 유저가 푼 방식

// 유저 1: 객체를 이용한 방식
function solution(rsp) {
    let arr = {
        2: 0,
        0: 5,
        5: 2
    };
    var answer = [...rsp].map(v => arr[v]).join("");
    return answer;
}

// 유저 2

function solution(rsp) {
    var answer = '';
    var arr = rsp.split('');
    for(i=0;i<arr.length;i++){
        if(arr[i]==='2'){
            answer += 0;
        }else if(arr[i]==='0'){
            answer += 5;
        }else{
            answer += 2;
        }
    }
    return answer;
}

 

 배운 것들

     -  정해진 답이 있을때는 객체를 이용하면 편하다.

     - 

+ Recent posts