본문 바로가기
알고리즘/Codewars

[Codewars] Find Maximum and Minimum Values of a List (8 kyu) / JavaScript

by fluss 2022. 10. 26.

https://www.codewars.com/kata/577a98a6ae28071780000989

 

Codewars - Achieve mastery through coding practice and developer mentorship

Coding practice for all programming levels – Join a community of over 3 million developers and improve your coding skills in over 55 programming languages!

www.codewars.com

 

DESCRIPTION:

Your task is to make two functions ( max and min, or maximum and minimum, etc., depending on the language ) that receive a list of integers as input, and return the largest and lowest number in that list, respectively.

 

Examples (Input -> Output)

* [4,6,2,1,9,63,-134,566]         -> max = 566, min = -134
* [-52, 56, 30, 29, -54, 0, -110] -> min = -110, max = 56
* [42, 54, 65, 87, 0]             -> min = 0, max = 87
* [5]                             -> min = 5, max = 5

 

Notes

  • You may consider that there will not be any empty arrays/vectors.

 

설명:

입력으로 정수의 리스트가 주어지고 리스트 안에서 가장 큰 숫자와 가장 작은 숫자를 반환하는 두함수(언어에 따라 max 와 min, 또는 maximum 그리고 minimun 등)를 만드세요.

 

예시 (입력 -> 출력)

* [4,6,2,1,9,63,-134,566]         -> max = 566, min = -134
* [-52, 56, 30, 29, -54, 0, -110] -> min = -110, max = 56
* [42, 54, 65, 87, 0]             -> min = 0, max = 87
* [5]                             -> min = 5, max = 5

 

참고

  • 빈 배열/벡터가 없을 것이라고 생각할 수 있습니다.

 

풀이

Math.min(), Math.max() 함수를 이용하여 풀었다.

 

코드

var min = function(list){
    return Math.min(...list);
}

var max = function(list){
    return Math.max(...list);
}

 

참고

https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Math/min

 

Math.min() - JavaScript | MDN

Math.min() 함수는 주어진 숫자들 중 가장 작은 값을 반환합니다.

developer.mozilla.org

 

https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Math/max

 

Math.max() - JavaScript | MDN

**Math.max()**함수는 입력값으로 받은 0개 이상의 숫자 중 가장 큰 숫자를 반환합니다.

developer.mozilla.org

 

댓글