⛵ 항해99/개강준비

[JavaScript 문법] 프로그래머스_2일차

hhhhy 2023. 7. 28. 16:28

 양꼬치 

  • 문제 주소

https://school.programmers.co.kr/learn/courses/30/lessons/120830

 

  • 나의 풀이
// 양꼬치값 : n * 12000
// 음료수 : k * 2000
// 서비스 음료수(양 10개 마다 1개) : n/10

function solution(n, k) {
    return (n * 12000 + k * 2000) - Math.trunc(n/10)*2000;
}
console.log(typeof solution(64, 6))

 

  • 알게 된 것
📍 Math.trunc() 메서드
- 소수 자릿수를 제거하여 숫자의 정수 부분을 반환

# 예시
console.log(Math.trunc(13.37));  // 13
console.log(Math.trunc(42.84));  // 42
console.log(Math.trunc(0.123));  // 0
console.log(Math.trunc(-0.123));  // -0

# 참고
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/trunc


📍 parseInt 활용한 코드
function solution(n, k) {
    return (n * 12000 + k * 2000) - parseInt(n / 10) * 2000;
}
console.log(typeof solution(64, 6))


📍 Math.trunc() / parseInt() 비교
https://www.samanthaming.com/tidbits/55-how-to-truncate-number/

 

 피자 나눠먹기(1) 

  • 문제 주소

https://school.programmers.co.kr/learn/courses/30/lessons/120814

 

  • 나의 풀이
function solution(n) {
    return Math.ceil(n/7);
}

console.log(solution(15))

 

  • 알게 된 것
📍 Math.ceil() 메서드
- 항상 반올림하여 주어진 숫자보다 크거나 같은 가장 작은 정수를 반환합니다.

# 예시
console.log(Math.ceil(.95));  // 1
console.log(Math.ceil(4));  // 4
console.log(Math.ceil(7.004));  // 8
console.log(Math.ceil(-7.004));  // -7

# 참고
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/ceil

 

 피자 나눠먹기(3) 

  • 문제 주소

https://school.programmers.co.kr/learn/courses/30/lessons/120816

 

  • 나의 풀이
function solution(slice, n) {
    return Math.ceil(n/slice);
}

 

 점의 위치 구하기 

  • 문제 주소

https://school.programmers.co.kr/learn/courses/30/lessons/120841

 

  • 나의 풀이
function solution(dot) {

    if (dot[0] > 0 && dot[1] > 0) {
        return 1;
    } else if (dot[0] < 0 && dot[1] > 0) {
        return 2;
    } else if (dot[0] < 0 && dot[1] < 0) {
        return 3;
    } else if (dot[0] > 0 && dot[1] < 0) {
        return 4;
    }
}

console.log(solution([2, 4]))

 

  • 알게 된 것
📍 지역변수 활용한 코드
function solution(dot) {
    var answer = 0;
    const x = dot[0];
    const y = dot[1];

    if (x > 0 && y > 0) answer = 1
    if (x < 0 && y > 0) answer = 2
    if (x < 0 && y < 0) answer = 3
    if (x > 0 && y < 0) answer = 4

    return answer;
}

console.log(solution([2, 4]))

 

 아이스 아메리카노 

  • 문제 주소

https://school.programmers.co.kr/learn/courses/30/lessons/120819

 

  • 나의 풀이
function solution(money) {
    // 머쓱이 돈 / 아메값 = 소수점 절삭 => 아메 수
    // 머쓱이 돈 - 아메값 * 아메수 => 머쓱이 잔돈

    var ame = Math.floor(money / 5500);
    var mschange = money - (5500 * ame);

    return [ame, mschange]
}

console.log(solution(15000))  // [2, 4000]

 

  • 알게 된 것
📍 나머지 연산자(%) 활용한 코드
function solution(money) {
    return [Math.floor(money / 5500), money % 5500];
}

console.log(solution(15000))  // [2,4000]


📍 나머지 연산자
- 왼쪽 피연산자를 오른쪽 피연산자로 나눴을 때의 나머지를 구함
- 부호는 항상 왼쪽 피연산자의 부호를 따름
- 남은 몫을 구하는 연산자임! 나눈 후 소숫점 반환하는 것과 혼동하지 말기!!

# 예시
console.log(13 % 5);  // 3
console.log(-13 % 5); // -3
console.log(4 % 2);  // 0
console.log(-4 % 2);  // -0

# 참고
https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Operators/Remainder

 

 옷가게 할인 받기 

  • 문제 주소

https://school.programmers.co.kr/learn/courses/30/lessons/120818

 

  • 나의 풀이
// 범위
~ 10만원 : 0%
10만원 ~ 30만원 : 5%
30만원 ~ 50만원 : 10%
50만원 ~ : 20%

function solution(price) {

    if (price < 100000) {
        return price;
    } else if (price >= 100000 && price < 300000) {
        return price - (price * 0.05);
    } else if (price >= 300000 && price < 500000) {
        return price - (price * 0.1);
    } else if (price >= 500000) {
        return price - (price * 0.2);
    }
}

console.log(solution(580000))

 

  • 알게된 것
📍 오류 : && 와 || 구분
- 10만원에서 30만원 사이는 &&로 작성해야함

# 오류코드
function solution(price) {

    if (price < 100000) {
        return price;
    } else if (💡price >= 100000 || price < 300000) {
        return price - (price * 0.05);
    } else if (💡price >= 300000 || price < 500000) {
        return price - (price * 0.1);
    } else if (price >= 500000) {
        return price - (price * 0.2);
    }
}

 제곱수 판별하기 

  • 문제 주소

https://school.programmers.co.kr/learn/courses/30/lessons/120909

 

  • 나의 풀이
function solution(n) {
    return Math.sqrt(n) % 1 === 0 ? 1 : 2;
}

# 풀이
제곱수가 아닐 경우, 1로 나눴을때 나머지(Math.sqrt(n) % 1)는 소수점이 나옴
console.log(Math.sqrt(3));  // 1.7320508075688772
console.log(Math.sqrt(3)%1);  // 0.7320508075688772

 

  • 알게 된 것
📍 Math.sqrt()
- 숫자의 제곱근을 반환
- 주어진 숫자에 루트(√ )를 씌우고, 만약 숫자가 음수이면 NaN를 반환

# 예시
Math.sqrt(9); // 3
Math.sqrt(2); // 1.414213562373095
Math.sqrt(1);  // 1
Math.sqrt(0);  // 0
Math.sqrt(-1); // NaN

# 참고
https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Math/sqrt


📍 거듭제곱 연산자 : **
- 왼쪽 피연산자를 밑, 오른쪽 피연산자를 지수로 한 값을 구함

# 예시
console.log(3 ** 4);  // 81
console.log(10 ** -2);  // 0.01
console.log(2 ** 3 ** 2);  // 512
console.log((2 ** 3) ** 2);  // 64

# 참고
https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Operators/Exponentiation