Practical2: Shopping Cart and calculation of averages
Mean, Median, and Mode
First we need some arrays of numbers on which to test our code.
var numbers1 = [1,2,3,4,5,6,7,8,9]; var numbers2 = [1,2,3,2,3,2,5,6,6,4,3,1,5,4,3]; var numbers3 = [1,2,3,1,2,3]; var mynumbers = [3,78,5,23,1,9,7,5];
Code
function mean(numberlist){
let sum = 0;
for (i=0 ; i<numberlist.length; i++)
sum += numberlist[i];
return sum/numberlist.length;
}
Test code
var numbers1 = [1,2,3,4,5,6,7,8,9];
var numbers2 = [1,2,3,2,3,2,5,6,6,4,3,1,5,4,3];
var numbers3 = [1,2,3,1,2,3];
var mynumbers = [3,78,5,23,1,9,7,5];
Results
The mean of numbers1 is 5.000
The mean of numbers2 is 3.333
The mean of numbers3 is 2.000
The mean of mynumbers is 16.375
Code
function median(numberlist){
var sortedList = numberlist.sort((a,b) => a-b);
var index = numberlist.length / 2;
if (Number.isInteger(index))
return (sortedList[index] + sortedList[index-1])/2;
else
return sortedList[Math.floor(index)];
}
Test code
var numbers1 = [1,2,3,4,5,6,7,8,9];
var numbers2 = [1,2,3,2,3,2,5,6,6,4,3,1,5,4,3];
var numbers3 = [1,2,3,1,2,3];
var mynumbers = [3,78,5,23,1,9,7,5];
Results
The mean of numbers1 is 5.000
The mean of numbers2 is 3.000
The mean of numbers3 is 2.000
The mean of mynumbers is 6.000
Code
// Mode solution produced after class
// As result can be bimodal or multi-modal,
// the returned result is provided as an array
// mode of [3, 5, 4, 4, 1, 1, 2, 3] = [1, 3, 4].
function mode(numberlist) {
let modes = [];
let count = [];
let number = 0;
let maxIndex = 0;
for (let i = 0; i < numberlist.length; i++) {
number = numberlist[i];
count[number] = (count[number] || 0) + 1;
if (count[number] > maxIndex) {
maxIndex = count[number];
}
}
// For - in statement.
// Push the number(s) with the highest appearance frequency
// into modes array.
for (let i in count) {
if (count[i] === maxIndex) {
modes.push(Number(i));
}
}
return modes;
}
Test code
var numbers1 = [1,2,3,4,5,6,7,8,9];
var numbers2 = [1,2,3,2,3,2,5,6,6,4,3,1,5,4,3];
var numbers3 = [1,2,3,1,2,3];
var mynumbers = [3,78,5,23,1,9,7,5];
Results
The mode of numbers1 is 1,2,3,4,5,6,7,8,9
The mode of numbers2 is 3
The mode of numbers3 is 1,2,3
The mode of mynumbers is 5
Shopping cart
To calcuate total prices and possible discounts, we first need to create some (fictional) shopping carts:
var cart1 = [
{name: 'apples', price: 1.20, quantity: 1, type: "food"},
{name: 'oranges', price: 1.05, quantity: 2, type: "food"},
{name: 'pens', price: 0.5, quantity: 3, type: "stationary"},
{name: 'A5 notebook', price: 1.50, quantity: 2, type: "stationary"}
]
var cart2 = [
{name: 'pears', price: 1.00, quantity: 2, type: "food"},
{name: 'salmon', price: 2.65, quantity: 1, type: "food"},
{name: 'thumb drive', price: 21.95, quantity: 1, type: "computing"},
{name: 'bucket', price: 1.50, quantity: 2, type: "home"},
{name: 'easter egg', price: 2.45, quantity: 3, type: "food"}
]
let shoppingCart = [
{
name:"loaf of bread",
type:"food",
quantity:1,
price:.85
},
{
name:"multipack beans",
type:"food",
quantity:2,
price:1
},
{
name:"mushrooms",
type:"food",
quantity:10,
price:.1
},
{
name:"can of beer",
type:"alcohol",
quantity:4,
price:1.1
},
{
name:"prosecco",
type:"alcohol",
quantity:1,
price:8.99
},
{
name:"steak",
type:"food",
quantity:2,
price:3.99
},
{
name:"blue cheese",
type:"food",
quantity:1,
price:2.99
},
{
name:"candles",
type:"home",
quantity:3,
price:1.99
},
{
name:"cheesecake",
type:"food",
quantity:1,
price:4.99
},
{
name:"onions",
type:"food",
quantity:3,
price:.4
},
];
Functions
Simple function to calculate the total cost of a shopping cart
function cartPrice(cart) {
let totalPrice = 0;
for (i=0 ; i<cart.length ; i++)
{
currentItem = cart[i];
totalPrice += currentItem.price * currentItem.quantity;
}
return totalPrice;
}
Pendantic function to calculate the total cost of a shopping cost. Also applies a 30% discount to food items
function shoppingTotal(cart) {
let totalPrice = 0;
for (let i=0 ; i<cart.length ; i++) {
let itemPrice = cart[i].price;
if (cart[i].type === 'food') {
itemPrice = .8 * itemPrice;
}
let itemQuantity = cart[i].quantity;
let itemTotalPrice = itemPrice * itemQuantity;
totalPrice = totalPrice + itemTotalPrice;
}
return totalPrice.toFixed(2);
}
More sophisticated function that takes a discount amount and the type of product to apply the discount to as arguments.
function shoppingTotalBetter(cart, discountAmount, type) {
let totalPrice = 0;
for (let i=0 ; i<cart.length ; i++) {
let itemPrice = cart[i].price;
if (cart[i].type === type) {
itemPrice = (100 - discountAmount)/ 100 * itemPrice;
}
let itemQuantity = cart[i].quantity;
let itemTotalPrice = itemPrice * itemQuantity;
totalPrice = totalPrice + itemTotalPrice;
}
return totalPrice.toFixed(2);
}
Finally a function to filter the cart based on price (with a random name). The variable quantity specifies whether the quantity of the item in the shopping basket should be taken into account in this calculation.
function ted(cart,lowPrice, highPrice, quantity){
let arrItems = [];
for (let i=0 ; i<cart.length; i++){
if (quantity === true) {
itemPrice = cart[i].price * cart[i].quantity;
}
else {
itemPrice = cart[i].price;
}
if (itemPrice >= lowPrice && itemPrice <= highPrice){
arrItems.push(cart[i]);
}
}
return arrItems;
}
Test Code
console.log('cart1 costs £'+cartPrice(cart1).toFixed(2));
console.log('cart2 costs £'+cartPrice(cart2).toFixed(2));
console.log('the combined cart costs £'+cartPrice(cart1.concat(cart2)).toFixed(2));
console.log('shoppingCart costs £'+cartPrice(shoppingCart).toFixed(2));
console.log('shoppingCart costs £'+shoppingTotal(shoppingCart));
console.log('shoppingCart with 20% discount on food costs £'+shoppingTotalBetter(shoppingCart, 20,'food'));
console.log('shoppingCart with 20% discount on home costs £'+shoppingTotalBetter(shoppingCart, 20,'home'));
console.log('shoppingCart with 1% discount on alcohol costs £'+shoppingTotalBetter(shoppingCart, 1,'alcohol'));
console.log('shoppingCart with 10% discount on home costs £'+shoppingTotalBetter(shoppingCart, 10,'food'));
console.log(ted(shoppingCart,2,5,true));
console.log(ted(shoppingCart,2,5,false));
Results
cart1 costs £7.80
cart2 costs £36.95
the combined cart costs £44.75
shoppingCart costs £40.37
shoppingCart costs £36.17
shoppingCart with 20% discount on food costs £36.17
shoppingCart with 20% discount on home costs £39.18
shoppingCart with 1% discount on alcohol costs £40.24
shoppingCart with 10% discount on home costs £38.27