⏱ 0:01est. 7 min
Shopify Coding Round 1
// Description
// The marketing team for a local grocery store wants to boost sales. To do so, they want to start using automatic discounts during checkout. The automatic discounts that they would like to have are:
//1. Buy one bag of grapes and get another bag of grapes for free ("Two for the price of one")
//2. Buy at least two apples and get a 20% discount on apples
// The products that are currently available in their store are:
// Bag of grapes priced at $5 per bag // 2 bag for price of one $5
// Apples priced at $3 per apple // 2 apples => 6-(6*.20) = 4.8
// Peaches priced at $7 per peach
// Your task: Build a program to calculate the total price at checkout for a customer, taking discounts into consideration.
// Behaviour
// The input to your solution should match the following schema: [ [ <product>, <quantity> ],...]
// The output of your solution should be a number representing the total price at checkout after discounts.
// Your solution should work for any combination of products listed above, in any quantities equal to or above 0. You do not need to handle unlisted products or quantities below 0.
// Input/Output Expectations
// Here are some example inputs and outputs. Note that the input does not have to look exactly like this. It can vary based on whatever is customary for the chosen language. For example, ruby commonly uses nested arrays but strongly typed languages like C#, Java, and C++ would commonly use lists of tuples. It is the spirit of the input that matters. Shape your input in whatever way makes sense for your language of choice. This isn't meant to be an input parsing challenge.
// # eg. in ruby
// [ ['grapes', 1], ['apples', 0], ['peaches', 1] ] # output => 12 //price
// [ ['grapes', 1], ['apples', 1], ['peaches', 1] ] # output => 15
// [ ['grapes', 2], ['apples', 2], ['peaches', 1] ] # output => 16.8
// [ ['grapes', 3], ['apples', 5], ['peaches', 2] ] # output => 36
// [ ['peaches', 7], ['grapes', 7], ['apples', 7] ] # output => 85.8
// [ ['grapes', 1], ['apples', 0], ['peaches', 1] ],
// [ ['grapes', 1], ['apples', 1], ['peaches', 1] ],
// [ ['grapes', 2], ['apples', 2], ['peaches', 1] ],
// [ ['grapes', 3], ['apples', 5], ['peaches', 2] ],
// [ ['peaches', 7], ['grapes', 7], ['apples', 7] ],
const _ = require('lodash');
function sayHello() {
console.log('Hello, World');
}
function getDiscountedPrice(totalPrice, name, quantity) {
let priceOfProducts = totalPrice;
if (name === 'grapes') {
// if (quantity%2==0) {
// priceOfProducts = priceOfProducts/2;
// } else {
let rem = (quantity%2) //(17/2=1) 1=5+*(16/2)*5
// if (rem===0) {
// priceOfProducts = priceOfProducts/2;
// } else {
priceOfProducts = rem*priceList[name].price
let remaining = quantity - rem;
priceOfProducts += (remaining*priceList[name].price)/2;
// }
// }
//quantity 2=5, 1=5
} else if (name === 'apples') {
if (quantity>=2) {
priceOfProducts -= (priceOfProducts*.20)
}
}
return priceOfProducts
}
// let input = [ ['grapes', 1], ['apples', 0], ['peaches', 1] ] // 12
// let input = [ ['grapes', 1], ['apples', 1], ['peaches', 1] ] // 12
// let input = [ ['grapes', 2], ['apples', 2], ['peaches', 1] ] // 7+3+5
let inputs = [
[ ['grapes', 1], ['apples', 0], ['peaches', 1] ],
[ ['grapes', 1], ['apples', 1], ['peaches', 1] ],
[ ['grapes', 2], ['apples', 2], ['peaches', 1] ],
[ ['grapes', 3], ['apples', 5], ['peaches', 2] ],
[ ['peaches', 7], ['grapes', 7], ['apples', 7] ]
]
const priceList = {
grapes: {price: 5},
apples: {price: 3},
peaches: {price: 7}
}
inputs.forEach((cart) => {
let cost = 0;
cart.forEach((product) => {
//product[0] name, product[1] quantity
let [name, quantity] = product;
// 3bags 10
let priceOfProducts = priceList[name].price * quantity;
let discountedPrice = getDiscountedPrice(priceOfProducts, name, quantity);
cost += discountedPrice
})
console.log(cost)
})
// _.times(5, sayHello);