Welcome to challenge 19! Use this thread for any and all questions relating to challenge 19.
Challenge 19 Megathread
Too soon for a golfed solution?
pumpkinSpice=m=>(x=[5,3,1].map(p=>(q=m/p|0,m%=p,q))).concat([30,15,3].reduce((a,v,i)=>a+v*x[i],0))
p, q, m
store the price, quotient, and money remaining.
In the reduce
function, a, v, i
respectively store the accumulator, current value, and current index.
When calculating the quotient, we use | 0
(bitwise OR) to truncate the number, saving characters because we don’t use Math.floor
or Math.trunc
.
const pumpkinSpice = money => {
let spice = 0;
const treats = {
"pie": {
"cost": 5,
"spice": 30,
"bought": 0
},
"latte": {
"cost": 3,
"spice": 15,
"bought": 0
},
"macaron": {
"cost": 1,
"spice": 3,
"bought": 0
}
}
for (let i in treats) {
while(money >= treats[i].cost) {
money = money - treats[i].cost;
spice += treats[i].spice;
treats[i].bought++;
}
}
return [treats.pie.bought, treats.latte.bought, treats.macaron.bought, spice];
}
const pumpkinSpice = money => {
const foods = {
"pie": {
"cost": 5,
"spice": 30
},
"lattes": {
"cost": 3,
"spice": 15
},
"macarons": {
"cost": 1,
"spice": 3
},
}
let totalSpice = 0;
return Object.keys(foods).reduce((acc, cv) => {
const moneyBefore = money;
totalSpice += Math.floor(money / foods[cv].cost) * foods[cv].spice;
money -= Math.floor(money / foods[cv].cost) * foods[cv].cost;
return [...acc, Math.floor(moneyBefore / foods[cv].cost)]
}, []).concat(totalSpice);
}
Might redo this one when I have time
const pumpkinSpice = money => {
var canbuy = [0,0,0,0]
var quotient = Math.floor(money/5);
var leftover = money-5quotient
var quotient2 = Math.floor(leftover/3);
var leftover = leftover-3quotient2
var quotient3 = Math.floor(leftover/1);
var alll = quotient*30 + quotient2*15 + quotient3 * 3
canbuy[0] = quotient
canbuy[1] = quotient2
canbuy[2] = quotient3
canbuy[3] = alll
return canbuy;
}
You always post very similar solutions before I can post mine, lol.
const pumpkinSpice = money => {
const C = {pie: {amt: 5, spice: 30},
latte: {amt: 3, spice: 15},
mac: {amt: 1, spice: 3}}
var Tspice = 0
let meal = Object.keys(C).reduce((acc,food) => {
let count=Math.floor(money / C[food].amt)
money %= C[food].amt
Tspice += count * C[food].spice
return [...acc, count]
},[])
return meal.concat(Tspice)
}
edit: fixed to make clearer
const pumpkinSpice = money => { // array of pies, lattes, macarons, total grams const pumpkinAmount = [0, 0, 0, 0]; while (money >= 5) { pumpkinAmount[0]++; pumpkinAmount[3] += 30; money -= 5; } while (money >= 3) { pumpkinAmount[1]++; pumpkinAmount[3] += 15; money -= 3; } while (money >= 1) { pumpkinAmount[2]++; pumpkinAmount[3] += 3; money -= 1; } return pumpkinAmount; }
Yay Math!
const pumpkinSpice = money => {
var pie=parseInt(money/5);
var remainder = money%5;
var latte = parseInt(remainder/3);
remainder = remainder%3;
var macaron = remainder
var grams = pie*30+latte*15+macaron*3
result = [pie, latte, macaron, grams];
return result;
}
I don’t know what the fuck is wrong again with my code. I run on my Chrome browser everything is ok
just a thought maybe add to the vars
var remainder;
var remainder2;
and use remainder2 in the macaron lines
what are macarons? I have been saying macaroons and now I’m lost. Just like “salumi” omg
I found the error. I miss the opening curly braces. Sorry for my lack of meticulousness. Is my mistake. I admit
Not a huge fan of how this one turned out.
const pie = {
price: 5,
spice: 30
}
const latte = {
price: 3,
spice: 15
}
const macaron = {
price: 1,
spice: 3,
}
const pumpkinSpice = money =>
[pie, latte, macaron].reduce(
(totals, { price, spice }, index) => {
// How many can we afford?
const amountPossible = Math.floor(money / price)
// Buy them all and spend the money.
totals[index] = amountPossible
money -= amountPossible * price
// Add the grams of spice.
totals[totals.length - 1] += amountPossible * spice
return totals
}, [ 0, 0, 0, 0 ])
const pumpkinSpice = money => {
const pumpkinPie = 5
const pumpkinLattes = 3
const pumpkinMacarons = 1
const result = [0,0,0,0]
while(money >= pumpkinMacarons){
if(money >= pumpkinPie){
result[0] += 1
result[3] += 30
money -= pumpkinPie
} else if(money >= pumpkinLattes){
result[1] += 1
result[3] += 15
money -= pumpkinLattes
} else if(money >= pumpkinMacarons){
result[2] += 1
result[3] += 3
money -= pumpkinMacarons
}
}
return result
}
Todays solution (no loops just math)
Solution
This challenge is perfect to use the modulo operator on %
Example: 5 % 2 // returns 1
Example 2:
let change = 9
change %= 5 // change will be 4
This allows us to keep track of the amount of money we have after buying each product.
Solution:
Summary
const pumpkinSpice = money => {
// Start keeping track of our money.
let remainder = money
// Calculate how many pies we can buy (which is the
// smallest rounded number if we divide by its cost.
let pies = Math.floor(remainder / 5)
// We use the modulo operator to calculate how much
// money we have left after buying pies.
remainder %= 5
// Do the same for the other products.
let lattes = Math.floor(remainder / 3)
remainder %= 3
let macarons = Math.floor(remainder / 1)
remainder %= 1
// Calculate the amount of spice we purchased
let grams = (pies * 30) + (lattes * 15) + (macarons * 3)
// Return everything we know :)
return [pies, lattes, macarons, grams]
}