2
I am trying to solve URI issue 1021 on JS, however I get 100% error.
Read a floating point value to two decimal places. This value represents a monetary value. Next, calculate the smallest number of possible banknotes and coins into which the value can be broken down. Banknotes are 100, 50, 20, 10, 5, 2. The possible currencies are 1, 0.50, 0.25, 0.10, 0.05 and 0.01. Show below the list of notes necessary.
Input The input file contains a floating point value N (0 N 1000000.00).
Output Print the minimum amount of banknotes and coins needed for exchange the initial value, as shown in the example provided.
Note: Use dot (.) to separate the decimal part.
My code:
var input = require('fs').readFileSync('/dev/stdin', 'utf8');
var lines = input.split('\n');
const valor = lines.shift();
const notas = [100, 50, 20, 10, 5, 2];
const moedas = [1.0, 0.5, 0.25, 0.1, 0.05, 0.01];
let n = parseFloat(valor);
console.log('NOTAS:');
notas.forEach(nota => {
console.log(`${parseInt(n / nota)} nota(s) de R$ ${nota.toFixed(2)}`);
n %= nota;
});
console.log('MOEDAS:');
moedas.forEach(moeda => {
console.log(`${parseInt(n / moeda)} moeda(s) de R$ ${moeda.toFixed(2)}`);
n %= moeda;
});
What is expected:
Exemplo de Entrada
576.73
Exemplo de Saída
NOTAS:
5 nota(s) de R$ 100.00
1 nota(s) de R$ 50.00
1 nota(s) de R$ 20.00
0 nota(s) de R$ 10.00
1 nota(s) de R$ 5.00
0 nota(s) de R$ 2.00
MOEDAS:
1 moeda(s) de R$ 1.00
1 moeda(s) de R$ 0.50
0 moeda(s) de R$ 0.25
2 moeda(s) de R$ 0.10
0 moeda(s) de R$ 0.05
3 moeda(s) de R$ 0.01
What am I doing wrong?
RESOLVED:
I checked on the URI forum that some people reported problems with other languages as well. I managed to solve by adding 0.00001 in the coin divisions to round:
n = (n % moeda) + 0.00001;
You are only doing pro first element of the array, right? Other than that, what output example does the program want?
– Felipe Avelar
Show a print of your output
– leofalmeida
I edited the post with this information.
– Ana Carolina Hernandes
You tested the program locally?
– Felipe Avelar