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

[Codewars] Find the smallest integer in the array (8 kyu) / JavaScript

by fluss 2022. 12. 5.

https://www.codewars.com/kata/55a2d7ebe362935a210000b2

 

Codewars - Achieve mastery through coding practice and developer mentorship

A coding practice website 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:

Given an array of integers your solution should find the smallest integer.

For example:

  • Given [34, 15, 88, 2] your solution will return 2
  • Given [34, -345, -1, 100] your solution will return -345

You can assume, for the purpose of this kata, that the supplied array will not be empty.

 

설명:

정수 배열이 주어지면 솔루션은 가장 작은 정수를 반환해야 합니다.

예를 들어:

  • [34, 15, 88, 2]이 주어지면 솔루션은 2를 반환해야 합니다.
  • [34, -345, -1, 100]이 주어지면 솔루션은 -345를 반환해야 합니다.

이 카타를 위해 제공된 배열이 비어있지 않은 것이라고 가정할 수 있습니다.

 

풀이

주어진 배열을 오름차순으로 정렬하고 가장 앞의 숫자를 반환했다.

 

코드

class SmallestIntegerFinder {
  findSmallestInt(args) {
    return args.sort((a, b) => a - b)[0];
  }
}

댓글