https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from
"String"
이라는 문자열을 받았을 때, ["S", "t", "r", "i", "n", "g"]
배열로 쪼개고 싶을 때 사용하는 메쏘드console.log(Array.from('foo'));
// Expected output: Array ["f", "o", "o"]
console.log(Array.from([1, 2, 3], (x) => x + x));
// Expected output: Array [2, 4, 6]
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce
array.reduce((accumulator, currentValue, index, array) => {
// 수행할 작업
}, initialValue);
const array1 = [1, 2, 3, 4];
// 0 + 1 + 2 + 3 + 4
const initialValue = 0;
const sumWithInitial = array1.reduce(
(accumulator, currentValue) => accumulator + currentValue,
initialValue,
);
console.log(sumWithInitial);
// Expected output: 10
let arr = ["1", "2", "3"]
return Math.min(...arr)+' '+Math.max(...arr);
굳이 문자 -> 숫자로 변형시켜주지 않아도 Math 함수 사용이 가능하다...
내가 푼 노가다
arr = arr.map((n) => Number(n));
let min = arr[0];
let max = arr[0];
for (let i = 1; i <= arr.length; i++) {
if (arr[i] < min) {
min = arr[i];
console.log('min', min);
}
if (arr[i] > max) {
max = arr[i];
console.log('max', max);
}