Algobook
- The developer's handbook
mode-switch
back-button
Buy Me A Coffee
Mon Jun 05 2023

How to create a quit smoking calculator in JavaScript

We all know that smoking is bad for us, but still most of us know at least one person in our circle of friends that are still going out to take a pinch of "air" during the day.

In todays guide, we will create a calculator that will figure out how much money and smokes that are consumed during one year dependent on some parameters - and also a second calculator that will tell how much that we can actually save if the person quits today.

Calculate yearly data

Let's start by calculating the yearly consumtion, in terms of money, smokes and also packs.

We will create a function, that will accept three parameters

  • dailySmokes
  • packSize
  • packCost

Then, we will simply multiply them and return an object with totalSpent, totalSmokes and totalPacks.

const costPerYear = (dailySmokes, packSize, packCost) => { const daysPerPack = packSize / dailySmokes; const totalPacks = Math.round(365 / daysPerPack); const totalSpent = packCost * totalPacks; const totalSmokes = dailySmokes * 365; return { totalSpent, totalSmokes, totalPacks }; };

The calculations are quite simple. For getting days per packs, we divide the size of each package with the daily consumtion of smokes. Total packs are the numbers of days per years divided by number of days per package and the total amount of money is the cost of each package multiplied by the total amount of packs per year.

Try it out

Let's get some numbers on the screen...

console.log(costPerYear(7, 19, 6.5)); // { totalSpent: 871, totalSmokes: 2555, totalPacks: 134 } console.log(costPerYear(10, 19, 6.5)); // { totalSpent: 1248, totalSmokes: 3650, totalPacks: 192 } console.log(costPerYear(15, 19, 6.5)); // { totalSpent: 1872, totalSmokes: 5475, totalPacks: 288 }

As seen, there is a quite a lot of money that are spent on those little bastards.

  • 7 smokes/day will equal $871/year
  • 10 smokes/day will equal $1248/year
  • 15 smokes/day will equal $1872 per year.

Calculate possible savings

If we want to give our friend a motivation to stop, we will create a new calculator that will take the same parameters, but also a yearsOfNoSmoke parameter that will represent the total years of no-smoking. Then we will calculate how many smokes that will not be smoked, total packs and also the total amount of savings for all years.

const possibleSavings = (dailySmokes, packSize, packCost, yearsOfNoSmoke) => { const yearly = costPerYear(dailySmokes, packSize, packCost); return { totalSmokes: dailySmokes * 365 * yearsOfNoSmoke, totalPacks: yearly.totalPacks * yearsOfNoSmoke, totalSavings: yearly.totalSpent * yearsOfNoSmoke, }; };

The calculations are also not that complicated here. We are using the previous function for getting 1 year worth of data, and then we simply multiply them with our fourth parameter yearsOfNoSmoke to get our total data.

Test it out

Let's give our friend some motivation...

console.log(possibleSavings(7, 19, 6.5, 10)); // { totalSmokes: 25550, totalPacks: 1340, totalSavings: 8710 } console.log(possibleSavings(10, 19, 6.5, 15)); // { totalSmokes: 54750, totalPacks: 2880, totalSavings: 18720 } console.log(possibleSavings(15, 19, 6.5, 20)); // { totalSmokes: 109500, totalPacks: 5760, totalSavings: 37440 }

So, not bad. 10 years will save us $8710, 15 years $18720 and 20 years $37440.

Summary

In this tutorial, we did some calculations on how much money people spend on smokes, and also how much they can save if they stop. I hope you enjoyed this article, and thanks for reading.

Don't forget to show you smoking friend these numbers ;)

Cheers.

signatureMon Jun 05 2023
See all our articles