TIL_230410-프로그래머스 Lv1, 해커랭크 Day 1

정윤숙·2023년 4월 10일
0

TIL

목록 보기
146/192
post-thumbnail

📒 오늘의 공부

1. 프로그래머스

Lv1. 정수 제곱근 판별

나의 풀이

function solution(n) {
    let checkN = Math.sqrt(n) 
    if(Number.isInteger(checkN) === true){
        return (checkN+1)**2
    }
    else{
        return -1
    }
}

다른 풀이

function nextSqaure(n){
  //함수를 완성하세요
  switch(n % Math.sqrt(n)){
    case 0:
      return Math.pow(Math.sqrt(n) + 1, 2);
    default:
      return "no"
  }
}
  • switch문 이용. 문제 개편 전이라 -1대신 'no' return한 듯

result = Number.isInteger(n) ? Math.pow(n+1, 2) : 'no';

  • 삼항연산자 이용

알게 된 것

Math.sqrt()

  • 제곱근 구하는 method

Math.pow()

  • 제곱 구하는 method

    const Num = 2
     const powNum = Math.pow(num, 3)
     console.log(powNum) // 8

    Number.isInteger()

  • 정수인지 확인하는 method

    • 정수라면 true 반환

2. 해커랭크

Day 1. Mini-Max Sum

나의 풀이

function miniMaxSum(arr) {
    const sortArr = arr.sort((a, b)=> a-b);
    const sumArr = sortArr.reduce((acc,cur)=> acc + cur)

    const minSum = sumArr - sortArr[arr.length-1];
    const maxSum = sumArr - sortArr[0];
    
    console.log(minSum, maxSum)

}
  • return을 했는데 아무것도 반환이 안 돼서 return 대신 console.log 찍어보니 문제가 풀림
profile
프론트엔드 개발자

0개의 댓글