백준-Node.js-2953, 나는 요리사다

송철진·2023년 2월 15일
0

백준-Node.js

목록 보기
27/69

풀이

const fs = require('fs')
const input = fs.readFileSync('/dev/stdin').toString().trim()
                .split('\n').map(el => el.split(' ')
                .reduce((a,b) => a + Number(b), 0))

const solution = (input) => {
  let max = Math.max(...input)
  input.forEach((el,i) => {
    if(el === max) console.log(i+1, max)
  })
}

solution(input)

reduce() 메서드를 사용하여 주어진 line마다 숫자의 합을 구한 배열을 input에 대해

['5 4 4 5','5 4 4 4','5 5 4 4','5 5 5 4','4 4 4 5']
👉 input = [ 18, 17, 18, 19, 17 ]

Math.max() 메서드와 스프레드 연산자로 input의 최댓값max를 구하고

input을 forEach() 메서드로 순회하여 max와 값이 일치하는 인덱스i+1과 max를 출력한다.

profile
검색하고 기록하며 학습하는 백엔드 개발자

0개의 댓글