.map– "Apply discounts and format prices"
Goal: Use Array.prototype.map to apply a 20% discount to every price, round each result to two decimal places, then chain a second .map to format each number as a currency string (prefix with "$").
Requirements:
- Create a new array
discountedwhere each price is reduced by 20% (price * 0.8). - Round each discounted price to two decimals.
- Chain a second
.mapto convert numbers to strings like"$9.60". - Log the final array and ensure original
pricesis unchanged.
Example input:
const prices = [12, 45, 23, 8, 90];Expected final output (example):
["$9.60", "$36.00", "$18.40", "$6.40", "$72.00"].filter– "Keep the weekend"
Goal: Use Array.prototype.filter to keep only days that start with the letter 'S', then map those names to uppercase.
Requirements:
- Use
.filterto select days whose first character is 'S' (case-sensitive for this exercise). - Chain
.mapto convert the filtered names to uppercase. - Return or log the final array of uppercase weekend days.
Example input:
const days = ['Mon','Tue','Wed','Thu','Fri','Sat','Sun'];Expected final output:
['SAT', 'SUN'].find– "First above average"
Goal: Compute the average of the scores, then use .find to locate the first score strictly greater than the average. Also return the index of that score using .findIndex.
Requirements:
- Compute the average from
scores. - Use
.findto get the first score > average. - Use
.findIndex(or.indexOf) to get that score's position in the original array. - Log both the found score and its index. If none found, explicitly log
undefined(or -1 for index).
Example input:
const scores = [67, 42, 89, 55, 73];Expected example output:
First above average: 89
Index of first above average: 2- "Mega-chain" – map → filter → map
Goal: Chain several array methods to produce greeting strings for users who have paid and are over 18. Then return only the first valid greeting.
Requirements:
- From
users, filter to those withpaid: trueandage > 18. - Map the filtered users to greeting strings like:
"Hello Ada — thanks for your purchase!". - Part A: Return the full array of greetings.
- Part B: Modify to return only the first greeting (use
.findor chain +[0]safely).
Example input:
const users = [
{name: 'Ada', age: 31, paid: true},
{name: 'Ben', age: 17, paid: false},
{name: 'Cara', age: 25, paid: true},
{name: 'Dan', age: 42, paid: false}
];Expected example outputs: Part A:
["Hello Ada — thanks for your purchase!", "Hello Cara — thanks for your purchase!"]Part B:
"Hello Ada — thanks for your purchase!"- DIY
groupBy– without.reduce
Goal: Implement a groupBy function that groups items by a provided key selector (no use of .reduce). After grouping, filter each customer's orders to keep only those with qty >= 3.
Requirements:
- Write
groupBy(array, keyFn)that returns an object whose keys are results ofkeyFn(item)and values are arrays of items. - After grouping
ordersbycustomer, transform each group's array to only include orders whereqty >= 3. - Return or log the filtered grouped result.
Example input:
const orders = [
{ customer: 'A', product: 'Book', qty: 2 },
{ customer: 'B', product: 'Game', qty: 1 },
{ customer: 'A', product: 'Game', qty: 3 },
{ customer: 'B', product: 'Book', qty: 4 }
];Expected final output:
{
A: [{ customer: 'A', product: 'Game', qty: 3 }],
B: [{ customer: 'B', product: 'Book', qty: 4 }]
}Starter skeleton (fill in implementation):
function groupBy(array, keyFn) {
const result = {};
// iterate array and push items into result[key]
return result;
}
// usage: groupBy(orders, o => o.customer)