iChasm
iChasm

Reputation: 1

Methods in Javascript - Code to find the amount of quarters in a value

Through Javascript using methods in as few lines as possible, how could I make a program count the number of quarters that could fit in a number?

For Example,

Example Input - quarters(1.22)

Answer: 4 Quarters .22 Cents

I want the "Quarters" answer to be a whole number and no fractions.

Psuedocode:

quarters = the integer of changedue/.25 changeDue is now = to the previous changeDue - (quarters times .25)

quarters(.72) //The ".72" is the value the user changes to whatever they want. the integer of .72/.25 = 2 changedue=.72-(2 x .25) or .72 - .50 =.12

Upvotes: 0

Views: 140

Answers (1)

Robby Cornelissen
Robby Cornelissen

Reputation: 97322

This is the most concise I can think of at the moment:

const quarters = (a) => `${Math.trunc(a * 4)} quarters ${a * 100 % 25} cents`;

console.log(quarters(1.22));

Upvotes: 2

Related Questions