x는 0부터시작 y는 10부터시작
5,5에서 멈추도록
1부터 10중에 3의배수만 빼고 출력해라
for (let i = 1; i <= 10; i++) {
if (i === 5) break;
console.log(i);
}
14 이하의 수에서 홀수만 출력
함수로 인자를 넣었을때 홀인지 짝인지
할당함수로 인자로 받는 수가 홀수인지 짝수인지, 2로 나눈 나머지로 조사해 트루인지 폴스인지 리턴
let person = {
name: '홍길동',
age: 30,
married: true,
introduce: function (formal) {
return formal
? '안녕하십니까. 홍길동 대리라고 합니다.'
: '안녕하세요, 홍길동이라고 해요.';
}
};
console.log(person.introduce(true));
console.log(person.introduce(false));
더하기 빼기 나누기 곱하기 함수를 가진 배열을 만들고 for문으로 5,3넣은것을 출력
let arithmetics = [
(a, b) => a + b,
(a, b) => a - b,
(a, b) => a * b,
(a, b) => a / b
];
for (arm of arithmetics) {
console.log( arm(5, 3));
}
객체의 프로퍼티에 접근하는법
저는 몇살이고 기혼입니다. 를 써봐라
name: '홍길동',
age: 30,
married: true,
고차함수, 콜백함수를 이용해서 [1,2,3,4,5] 에 있는것들이 출력되는걸 해라
익명함수로 3 , 5, 3 로
곱하기 나누기로 해서
함수의 결과값을 받는걸 다시 for안의 함수로 넣어서
40 0.625
값이 나오도록 해라
더하기 빼기 곱하기 함수들을 인자, 홀수인지 양수인지 판단하는 함수를 인자, 로 넣어서 5,7로 만들어봐라
function을 결과값으로 반환할 수 있는 함수를 만들어라. boolean을 인자값으로 받고 그거에 따라 삼항 연산자로 펑션 둘중 하나 고르는거
이걸로 함수 안에 리턴값이 함수이면 어떻게 호출하는지도 보여줘라
플로스 마이너스 곱하기 나누기를
한 함수 안에서 리턴값으로 두 인자를 받아서 나오게 만들어라
(a+b)*c-d를 커링함수로 작성
// 기존의 코드
function addMultSubt (a, b, c, d) {
return (a + b) * c - d;
}
const addMultSubt2 = (a, b, c, d) => (a + b) * c - d;
console.log(
addMultSubt(2, 3, 4, 5),
addMultSubt2(2, 3, 4, 5),
);
// ⭐ 커링으로 작성된 함수
function curryAddMultSubt (a) {
return function (b) {
return function (c) {
return function (d) {
return (a + b) * c - d;
}
}
}
}
const curryAddMultSubt2 = a => b => c => d => (a + b) * c - d;
console.log(
curryAddMultSubt(2)(3)(4)(5),
curryAddMultSubt2(2)(3)(4)(5)
);
const curryAddMultSubtFrom2 = curryAddMultSubt(2);
const curryMultSubtFrom5 = curryAddMultSubt(2)(3);
const currySubtFrom20 = curryAddMultSubt(2)(3)(4);
console.log(curryAddMultSubtFrom2);
console.log(curryMultSubtFrom5);
console.log(currySubtFrom20);
// 화살표 함수로 바꾸어 다시 실행해 볼 것
console.log(
curryAddMultSubtFrom2(3)(4)(5),
curryMultSubtFrom5(4)(5),
currySubtFrom20(5)
);
기본값이 있는 add에 매개변수 없음, 1, 1,3 이 있게 해서
function add(a = 2, b = 4) {
console.log(`${a} + ${b}`);
return a + b;
}
console.log(
add(),
add(1),
add(1, 3)
);
여기에 들어있는
1357 을 arguments로 하나하나 나오게 하고
그 [0]을 나오게하고 arguments의 type를 알아내라.
add()가 인자를 두개만 받는데 인자를 4개를 넣어버림. 그러면 그안에 있는거 다출력하고 a+b 리턴하는걸 만들어라
arguments.length를 이용해서
1,4,7의 평균
24,31,52,80 dml vudrbs
그것을 또 타입에 안전하게 한 버전
주함수는 인자가 없는데 함수만 잇도록 걸래나고 첫번재 인자와 두번째 인자를 함수, // 그 다음꺼를 또함수// 그 다음꺼롤 또함수
이런식으로 만들어라
const add = (a, b) => a + b;
const sub = (a, b) => a - b;
const mul = (a, b) => a * b;
const div = (a, b) => a / b;
function combineArms () {
return (x, y) => {
let result = x;
for (const arm of arguments) {
if (typeof arm !== 'function') continue;
result = arm(result, y);
}
return result;
}
}
const add_mul = combineArms(add, mul, 1, true);
const add_mul_sub = combineArms(add, mul, sub);
const add_mul_sub_div = combineArms(add, mul, sub, div);
// 💡 익명 함수 사용됨
const add_mul_sub_div_pow
= combineArms(add, mul, sub, div, (x, y) => x ** y);
console.log(
add_mul(8, 3),
add_mul_sub(8, 3),
add_mul_sub_div(8, 3),
add_mul_sub_div_pow(8, 3)
);
이렇게 출력되게 해봐라
console.log(
'3.',
classIntro(3, '김민지', '영희', '철수', '보라')
); // 호이스팅
function classIntro (classNo, teacher, ...children) {
console.log('1.', children);
console.log('2.', arguments);
let childrenStr = '';
for (const child of children) {
if (childrenStr) childrenStr += ', ';
childrenStr += child;
}
return `${classNo}반의 선생님은 ${teacher}, `
+ `학생들은 ${childrenStr}입니다.`
}
주함수는 인자가 없는데 함수만 잇도록 걸래나고 첫번재 인자와 두번째 인자를 함수, // 그 다음꺼를 또함수// 그 다음꺼롤 또함수
이런식으로 만들어라
위위문제와 똑같은건데
const add_mul = combineArms(add, mul, 1, true);
const add_mul_sub = combineArms(add, mul, sub);
const add_mul_sub_div = combineArms(add, mul, sub, div);
이거 안해도 되고
이것만 해도 될수있게..
... 넣어서 할 수 있게 해라
const add = (a, b) => a + b;
const sub = (a, b) => a - b;
const mul = (a, b) => a * b;
const div = (a, b) => a / b;
function doMultiArms (x, y, ...arms) {
let result = x;
for (const arm of arms) {
if (typeof arm !== 'function') continue;
result = arm(result, y);
}
return result;
}
console.log(
doMultiArms(8, 3, add, mul, 1, true),
doMultiArms(8, 3, add, mul, sub),
doMultiArms(8, 3, add, mul, sub, div),
doMultiArms(8, 3, add, mul, sub, div, (x, y) => x ** y)
);
중첩함수를 한번 만들어보아라 cl
이렇게나오게
재귀함수를 한번 만들어봐라
재귀함수로 팩토리얼 한번 만들어봐라
즉시실행함수 써서
8월 1일의 평균기온은 XX도입니다 이거 출력하게 해라
{}에서 키값을 객체 또는 배열로 해서
프로퍼티리를 꺼내는걸 한번 해봐라
객체 선언시 프로퍼티 키와 대입할 상수 변수 명이 동일할때
객체 안에 메서드를 불러오는거
댓글 영역