코드로 우주평화

Highest and Lowest 본문

나는 이렇게 학습한다/Algorithm & SQL

Highest and Lowest

daco2020 2022. 3. 8. 22:34
반응형

Description:

In this little assignment you are given a string of space separated numbers, and have to return the highest and lowest number.

Examples

highAndLow("1 2 3 4 5");  // return "5 1"
highAndLow("1 2 -3 4 5"); // return "5 -3"
highAndLow("1 9 3 4 -5"); // return "9 -5"

Notes

  • All numbers are valid Int32, no need to validate them.
  • There will always be at least one number in the input string.
  • Output string must be two numbers separated by a single space, and highest number is first.

 

 

Solution:

1. Make the 'numbers' string into an array.
2. Make the elements of the array a number.
3. Find the maximum and minimum values ​​in an array.
4. Returns the maximum and minimum values ​​as strings.

 

function highAndLow(numbers){
  numbers = numbers.split(' ').map(number => Number(number))
  let max = Math.max(...numbers)
  let min = Math.min(...numbers)
  return `${max} ${min}`
}

 

 

 

반응형

'나는 이렇게 학습한다 > Algorithm & SQL' 카테고리의 다른 글

Testing 1-2-3  (0) 2022.03.10
Find the middle element  (0) 2022.03.09
Who likes it?  (0) 2022.03.07
Abbreviate a Two Word Name  (0) 2022.03.06
Equal Sides Of An Array  (0) 2022.03.05