๐ Scenario: You are working on a small app that calculates prices after discount for a list of products. You want to use arrow functions to make your code shorter and easier to read.
๐ฏ Goal: Build a simple JavaScript program that uses arrow functions to calculate discounted prices for products.
๐ What You'll Learn
Create an array of product prices.
Create a discount rate variable.
Use an arrow function to calculate discounted prices.
Print the discounted prices array.
๐ก Why This Matters
๐ Real World
Arrow functions are used in many JavaScript projects to write cleaner and shorter code, especially when working with arrays and callbacks.
๐ผ Career
Understanding arrow functions is important for front-end and back-end JavaScript development jobs, as they are a common modern syntax.
Progress0 / 4 steps
1
Create the product prices array
Create an array called prices with these exact numbers: 100, 200, 300, 400, 500.
Javascript
Hint
Use square brackets [] to create an array and separate numbers with commas.
2
Create the discount rate variable
Create a constant called discountRate and set it to 0.1 (which means 10%).
Javascript
Hint
Use const to create a variable that does not change.
3
Use an arrow function to calculate discounted prices
Create a new array called discountedPrices by using the map method on prices. Use an arrow function with parameter price that returns price minus price times discountRate.
Javascript
Hint
The arrow function looks like price => price - price * discountRate.
4
Print the discounted prices
Use console.log to print the discountedPrices array.
Javascript
Hint
Use console.log(discountedPrices) to see the result in the console.
Practice
(1/5)
1. What is the main advantage of using arrow functions in JavaScript?
easy
A. They replace all regular functions in JavaScript.
B. They provide a shorter syntax for writing functions.
C. They can only be used for asynchronous code.
D. They automatically create new objects.
Solution
Step 1: Understand arrow function purpose
Arrow functions are designed to write functions in a shorter way compared to regular function expressions.
Step 2: Compare options
Only They provide a shorter syntax for writing functions. correctly states the main advantage: shorter syntax. Other options are incorrect or misleading.
Final Answer:
They provide a shorter syntax for writing functions. -> Option B
Quick Check:
Arrow functions = shorter syntax [OK]
Hint: Arrow functions are mainly about shorter syntax [OK]
Common Mistakes:
Thinking arrow functions create new objects automatically
Believing arrow functions only work with async code
Assuming arrow functions replace all regular functions
2. Which of the following is the correct syntax for an arrow function that adds two numbers a and b?
easy
A. const add = (a, b) => { a + b; };
B. const add = (a, b) { return a + b; };
C. const add = a, b => a + b;
D. const add = (a, b) => a + b;
Solution
Step 1: Check arrow function syntax
Arrow functions can omit braces and return keyword for single expressions, so (a, b) => a + b is valid.
Step 2: Analyze options
A: concise and correct with implicit return. B: missing => operator, syntax error. C: misses parentheses around parameters. D: lacks return statement inside braces.
Final Answer:
const add = (a, b) => a + b; -> Option D
Quick Check:
Concise arrow function = (a, b) => a + b [OK]
Hint: Use parentheses for multiple params and omit braces for single expression [OK]
Common Mistakes:
Omitting parentheses when multiple parameters exist