Programming (69) 썸네일형 리스트형 Coding Challenge #1 - who's win? const calcAverage = (a, b, c) => (a + b + c) / 3; const scoreDolphins = calcAverage(85, 54, 41); const scoreKoalas = calcAverage(23, 34, 27); console.log(scoreDolphins, scoreKoalas); const checkWinner = function (avgDolhins, avgKoalas) { if (avgDolhins >= 2 * avgKoalas) { console.log(`Dolphins win (${avgDolhins} vs. ${avgKoalas})`); } else if (avgKoalas >= 2 * avgDolhins) { console.log(`Koalas w.. Reviewing Functions Summary - Function의 기본적인 동작 원리: receive input data, transform data, and then output data - Parameters: placeholders to receive input values. Like local variables of a function - Function body: block of code that we want to reuse. Processes the function's input data - return statement to output a value from the function and terminate execution - Calling, running or invoking the function, using ().. Functions Calling Other Functions function cutFruitPieces(fruit) { return fruit * 4; } function fruitProcessor(apples, oranges) { const applePieces = cutFruitPieces(apples); const orangePieces = cutFruitPieces(oranges); const juice = `Juice with ${applePieces} piece of apple and and ${orangePieces} pieces of orange.`; return juice; }; console.log(fruitProcessor(2, 3)); // prints Juice with 8 piece of apple and and 12 pieces of o.. Arrow Functions // Arrow function const calcAge3 = birthYear => 2037 - birthYear; const age3 = calcAge3(1988); console.log(age3); const yearsUntilRetirement = (birthYear, firstName) => { const age = 2037 - birthYear; const retirement = 65 - age; // return retirement; return `${firstName} retires in ${retirement} years`; } console.log(yearsUntilRetirement(1988, 'Sunny')); // prints Sunny retires in 16 years Function Declarations vs. Expressions Function declaration은 function을 declare하기 전에 call할 수 있음 Expression은 그렇게 안됨 🙅♀️ Expression은 declare하기 전에 해당 function을 call하면 에러 뜸 e.g. Uncaught ReferenceError: Cannot access 'OOO' before initialization // Function declaration function calcAge1(birthYear) { return 2037 - birthYear; } const age1 = calcAge1(1991); // Function expression // Expression produces value // Functions are not type, but va.. Activating Strict Mode The "use strict" Directive It is not a statement, but a literal expression, ignored by earlier versions of JavaScript. The purpose of "use strict" is to indicate that the code should be executed in "strict mode". With strict mode, you can not, for example, use undeclared variables. Strict Mode는 말그대로 코드가 strict mode에서 실행되어야 함을 의미함 그럼 여기서 말하는 strict mode란? 선언되지 않은 variable을 사용하면 안됨 🙅♀️ JavaScript.. Functions function을 쓰는 이유? 같은 코드를 반복해서 사용해야 하는 상황에서 해당 코드를 function으로 만들어서 call하면 reusable해짐! 이런 코드를 dry/ clean code라고 함! function을 안쓰면? 매번 manually 한땀한땀 노가다 해줘야함.. 효율적이지 않음 🙅♀️ function logger() { console.log('My name is Sunny 🌞'); } // calling / running/ invoking function logger(); logger(); logger(); function fruitProcessor(apples, oranges) { // console.log(apples, oranges); const juice = `Juice with .. Coding Challenge #4 - tip calculator using ternary operator const billValue = 430; const tip = billValue >= 50 && billValue The Conditional (Ternary) Operator ternary: composed of three parts The ternary operator evaluates the test condition. If the condition is true , expression1 is executed. If the condition is false , expression2 is executed. https://www.programiz.com/javascript/ternary-operator JavaScript Ternary Operator (with Examples) A ternary operator can be used to replace an if..else statement in certain situations. Before you learn about t.. Statements and Expressions A JavaScript program is a sequence of statements. Each statement is an instruction for the computer to do something. vs. An expression is a bit of JavaScript code that produces a value. 1 → produces 1 "hello" → produces "hello" 5 * 10 → produces 50 num > 100 → produces either true or false isHappy ? "🙂" : "🙁" → produces an emoji [1, 2, 3].pop() → produces the number 3 https://www.joshwcomeau.com.. 이전 1 2 3 4 5 6 7 다음