Arrays
An array is an ordered list of values, written in square brackets:
const fruits = ["mango", "banana", "guava"];
console.log(fruits[0]); // mango (counting starts at 0)
console.log(fruits.length); // 3
fruits.push("orange"); // add to the end
You can loop over an array with for...of:
for (const fruit of fruits) {
console.log(fruit);
}
map, filter and reduce
Three array methods do most of the everyday work. Each takes a small function, usually written as an arrow function: x => x * 2 means "given x, give back x times 2".
const numbers = [1, 2, 3, 4];
numbers.map(n => n * 10); // [10, 20, 30, 40] change every item
numbers.filter(n => n % 2 === 0); // [2, 4] keep some items
numbers.reduce((sum, n) => sum + n, 0); // 10 combine into one value
reduce starts with the value after the comma (0) and feeds each item in turn into the function, carrying the running result along.
map and filter return new arrays; the original stays as it was.
Your task
Write three functions that each take an array of prices:
total(prices): the sum of all prices (0 for an empty array).cheap(prices): only the prices below 100.withTax(prices): every price plus 15%, rounded to 2 decimal places.