본문 바로가기

Javascript40

반복문 없이 반복문 하기 반복문 없이 반복문 하는 방법이 있어 공유합니다. const scores = [85, 95, 76]; const average = (list, total, length) => { if (length === list.length) return total / length; return average(list, total + list[length], length + 1); }; console.log('average:', average(scores, 0, 0)); 위처럼 하면 if (length === list.length) 가 되기 전까지는 return 문을 수행하지 않으며 총 Length에서 total 값을 나눠 평균값 구한 값을 리턴하지 않습니다. 그러니까. 그렇게 되기까지 average를 리턴하는데. 그.. 2019. 9. 14.
Javscript Callback function sum(a, b) { return a + b; } const printResult = (result) => { console.log('결과는 ', result, '입니다.'); }; const calculationAndPrint = (calculationResult, callback) => { callback(calculationResult); }; calculationAndPrint(sum(10, 20), printResult); 2019. 9. 12.
Javascript 함수가 왜 1급 객체일까? Javascript 함수는 1급 객체라고 일컬어지는데요.. 함수를 변수나 데이터에 할당도 할 수 있고 파라미터로 값을 집어넣어 함수의 선언된 문을 수행한 뒤에 리턴까지 할 수 있습니다. function plus(a, b) { return a + b; } function minus(a, b) { return a - b; } const p = plus; console.log('typeof plus : %s', typeof plus); console.log('typeof p : %s', typeof p); console.log('10 + 20 = %d', p(10, 20)); // 함수를 파라메터로 받는 함수 function calculate(a, b, func) { return func(a, b); } //.. 2019. 9. 12.
[JS] JS 5가지 팁 [JS] JS 5가지 팁1. Array.includes 1234567891011121314151617// conditionfunction test(fruit) { if (fruit == 'apple' || fruit == 'strawberry') { // 다른 과일이 늘어나면?????? console.log('red'); }} // using Array.includesfunction test(fruit) { // extract conditions to array const redFruits = ['apple', 'strawberry', 'cherry', 'cranberries']; if (redFruits.includes(fruit)) { console.log('red'); }}Colored by Col.. 2019. 2. 1.