2022/12/25
- (Lv. 0) 피자 나눠 먹기 (1)
- (Lv. 0) 피자 나눠 먹기 (2)
- (Lv. 0) 피자 나눠 먹기 (3)
- (Lv. 0) 배열의 평균값
- (Lv. 0) 옷가게 할인 받기
- (Lv. 0) 아이스 아메리카노
- (Lv. 0) 나이 출력
- (Lv. 0) 배열 뒤집기
정수 배열 numbers가 매개변수로 주어집니다. numbers의 원소의 평균값을 return하도록 solution 함수를 완성해주세요.
✨
reduce()
: 배열(arr)의 각 element들에 대해서 파라미터로 입력받은 callback 함수를 실행하여 하나의 리턴값을 반환하는 함수const sum = arr.reduce((a,b) => (a+b));
function solution(numbers) {
const avg = numbers.reduce((a,b) => (a+b))/numbers.length;
return avg;
}
정수 배열 numbers가 매개변수로 주어집니다. numbers의 원소의 평균값을 return하도록 solution 함수를 완성해주세요.
function solution(price) {
if (price>=500000)
price = parseInt(price*0.8);
else if (price>=300000)
price = parseInt(price*0.9);
else if (price>=100000)
price = parseInt(price*0.95);
return price;
}
const discounts = [
[500000, 20],
[300000, 10],
[100000, 5],
]
const solution = (price) => {
for (const discount of discounts)
if (price >= discount[0])
return Math.floor(price - price * discount[1] / 100)
return price
}
✨
~ (tilde)
: NOT 비트 연산자
✨~~ (double tilde)
: 이중 NOT 의 비트 연산자
-> 양수에 대해서는Math.floor()
과 같고 음수에 대해서는Math.ceil()
과 같은 결과
function solution(price) {
price = price>=500000?price*0.8:price>=300000?price*0.9:price>=100000?price*0.95:price;
return ~~(price);
}
정수가 들어 있는 배열 num_list가 매개변수로 주어집니다. num_list의 원소의 순서를 거꾸로 뒤집은 배열을 return하도록 solution 함수를 완성해주세요.
function solution(num_list) {
var answer = [];
for (let i=0;i<num_list.length;i++) {
answer.unshift(num_list[i])
}
return answer;
}
✨
unshift()
: 배열의 앞쪽에 요소 추가
return num_list.reverse()
✨
reverse()
: 배열 뒤집기
❗️ 이 함수를 실행시키면 원본 배열이 변형된다
쉬운 문제를 풀고 있는만큼 더 효율적이고 js스럽게 풀자!
자꾸 파이썬 문법, 플로우 사용해서 에러 ...