1. 다음에 올 숫자(프로그래머스 LV.0)

yesolog·2022년 11월 21일
0

Algorithm

목록 보기
1/10
post-thumbnail

⬜ 문제 설명

등차수열 혹은 등비수열 common이 매개변수로 주어질 때, 마지막 원소 다음으로 올 숫자를 return 하도록 solution 함수를 완성해보세요.

⬜ 제한 사항

⚫ 2 < common의 길이 < 1,000
⚫ -1,000 < common의 원소 < 2,000
⚫ 등차수열 혹은 등비수열이 아닌 경우는 없습니다.
⚫ 공비가 0인 경우는 없습니다.

⬜ 나의 풀이

function solution(common){
  var answer = 0;
  if(common[1]-common[0] == common[2]-common[1]){
    answer = common[common.length -1] +(common[1]-common[0])
    console.log(answer)
  } else {
    answer = common[common.length-1] * (common[1] / common[0])
  }
  return answer;
}

⬜ 문풀 포인트

⚫ 인덱스 값이 무엇인지 정확하게 알기
⚫ 배열의 마지막 값을 추출하는 방법 알기.

ex)

arr = [1 , 2, 3, 4]
//마지막 값 추출을 원하는 상황
const x = arr[arr.length -1]
console.log(x) // 4

0개의 댓글