Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- redux
- 테코테코
- SW
- react
- 항해플러스
- 항해99
- maeil-mail
- java
- Python
- useDispatch
- JavaScript
- C++
- Get
- redux-toolkit
- 알고리즘
- Algorithm
- sw expert academy
- 이코테
- 프로그래머스
- 매일메일
- 리액트
- redux-saga
- 자바
- programmers
- json-server
- react-redux
- axios
- react-router
- 코딩테스트합격자되기
- createSlice
Archives
- Today
- Total
Binary Journey
[Javascript][메모] 프로그래머스 음양 더하기 본문
반응형
https://programmers.co.kr/learn/courses/30/lessons/76501
코딩테스트 연습 - 음양 더하기
어떤 정수들이 있습니다. 이 정수들의 절댓값을 차례대로 담은 정수 배열 absolutes와 이 정수들의 부호를 차례대로 담은 불리언 배열 signs가 매개변수로 주어집니다. 실제 정수들의 합을 구하여 re
programmers.co.kr
출처: 프로그래머스 코딩 테스트 연습, https://programmers.co.kr/learn/challenges
[숫자들], [Boolean들] 을 받았을 때 Boolean 배열이 true 이면 더하고 false이면 빼는 제목 그대로 음양 더하기이다.
reduce를 사용할까 했는데 그냥 for문을 돌렸다.
function solution(absolutes, signs) {
let answer = 0;
for (const index in signs) {
if (!signs[index]) {
absolutes[index] = absolutes[index] * -1;
}
answer += absolutes[index]
}
return answer;
}
역시나 가장 높은 추천수를 받은 답안은 reduce를 썼다.
function solution(absolutes, signs) {
return absolutes.reduce((acc, val, i) => acc + (val * (signs[i] ? 1 : -1)), 0);
}
반응형
'프로그래머스 > 메모' 카테고리의 다른 글
[Javascript][메모] 프로그래머스 이진변환 반복하기 (0) | 2021.06.08 |
---|---|
[Javascript][메모] 프로그래머스 3진법 (0) | 2021.06.08 |
[Javascript][메모] 프로그래머스 피보나치 수, 2 x n 타일링 (0) | 2021.04.22 |