SMALL
1. 조건에 맞게 수열 변환하기 1
function solution(arr) {
var answer = [];
let newI = 0;
arr.forEach( i => {
if (i%2==0 && i>=50) {
newI = i/2;
} else if (i%2==1 && i<50) {
newI = i*2;
} else {
newI = i;
}
answer.push(newI);
})
return answer;
}
2. 1로 만들기
function solution(num_list) {
var answer = 0;
let cnt = 0;
num_list.forEach(i => {
while (i!=1) {
if (i%2==0) {
i/=2;
} else {
i = (i-1)/2;
}
cnt++;
}
})
answer = cnt;
return answer;
}
728x90
'Algorithm > Javascript' 카테고리의 다른 글
[프로그래머스] 230725 코딩테스트 연습 (0) | 2023.07.25 |
---|---|
[프로그래머스] 230724 코딩테스트 연습 (0) | 2023.07.24 |
[프로그래머스] 230721 코딩테스트 연습 (0) | 2023.07.21 |
[프로그래머스] 230720 코딩테스트 연습 (0) | 2023.07.20 |
[프로그래머스] 230718 코딩테스트 연습 (0) | 2023.07.18 |
댓글