본문 바로가기

Programming/JavaScript

(65)
Coding Challenge #3 - comparing BMIs using objects Tried 👏👏👏 const Mark = { fullName: 'Mark Miller', mass: 78, height: 1.69, calcBMI: function () { this.bmi = this.mass / (this.height * this.height); return this.bmi.toFixed([1]); } } const John = { fullName: 'John Smith', mass: 92, height: 1.95, calcBMI: function () { this.bmi = this.mass / (this.height * this.height); return this.bmi.toFixed([1]); } } function compareBmi() { whoHasHigherBmi = M..
Object Methods const sunny = { firstName: 'Sunny', lastName: 'Ryu', birthYear: 1988, job: 'technical writer', hobbies: ['Writing', 'Coding', 'Learning'], hasDriversLicense: true, calcAge: function (birthYear) { return 2022 - birthYear; } }; console.log(sunny.calcAge(1988)); // prints 34 console.log(sunny['calcAge'](1988)); // prints 34 Don't repeat by yourself principle 위에서 보면 birthYear 1988을 2번 이상 반복해서 사용함 근데..
Dot vs. Bracket Notation const sunny = { firstName: 'Sunny', lastName: 'Ryu', age: 2022 - 1988, job: 'technical writer', hobbies: ['Writing', 'Coding', 'Learning'] }; console.log(sunny); 코드에 작성한 property 순서와는 상관없이 automatically ordered됨 참고로 dot notation에서는 real final name이 필요함 e.g.) sunny.'last' + nameKey 🙅‍♀️ console.log(sunny.lastName); console.log(sunny['lastName']); // prints Ryu firstName이랑 lastName에서 Name이 중복되니 na..
Introduction to Objects In JavaScript, an object is an unordered collection of key-value pairs. Each key-value pair is called a property. The key of a property can be a string. And the value of a property can be any value, e.g., a string, a number, an array, and even a function. JavaScript provides you with many ways to create an object. https://www.javascripttutorial.net/javascript-objects/ JavaScript Objects In this ..
Coding Challenge #2 - tip calculator with arrays Tried 👏👏👏 const tips = [125, 555, 44]; const calcTip = function (billValue) { if (billValue >= 50 && billValue = 50 && bill bill >= 50 && billl
Basic Array Operations (Methods) const friends = ['Michael', 'Steven', 'Peter']; friends.push('Jay'); console.log(friends); // prints ['Michael', 'Steven', 'Peter', 'Jay'] Add elements const friends = ['Michael', 'Steven', 'Peter']; const newLength = friends.push('Jay'); console.log(newLength); // prints 4 friends.unshift('John'); console.log(friends); // prints ['John', 'Michael', 'Steven', 'Peter', 'Jay'] unshift() method is ..
Introduction to Arrays const friend1 = 'Michael'; const friend2 = 'Steven'; const friend3 = 'Peter'; const friends = ['Michael', 'Steven', 'Peter']; console.log(friends); const y = new Array(1991, 1984, 2008, 2020); console.log(friends[0]); console.log(friends[2]); console.log(friends.length); console.log(friends[friends.length - 1]); friends[2] = 'Jay'; console.log(friends); const firstName = 'Jonas'; const jonas = [..
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..