Skip to content

Instantly share code, notes, and snippets.

@SeonghuiChoe
Last active June 20, 2017 05:51
Show Gist options
  • Save SeonghuiChoe/6f533f75b8ab32bb225d32d9d30a2632 to your computer and use it in GitHub Desktop.
Save SeonghuiChoe/6f533f75b8ab32bb225d32d9d30a2632 to your computer and use it in GitHub Desktop.
javascript 문제 세번째
/*
* 문자열에서 최소값과 최대값 출력
* ex) "1 2 3 4" => "1 4"
*/
var input = '1 2 33 4';
var arr = input.match(/\d+/g).map(n => parseInt(n));
var arrMinMax = arr.reduce((pre, curr) => {
var min = curr < pre[0] ? curr : pre[0];
var max = curr > pre[1] ? curr : pre[1];
return [min, max];
}, [arr[0], arr[0]]);
console.log('input:', input);
console.log('min:', arrMinMax[0]);
console.log('max:', arrMinMax[1]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment