URI Online Judge 1021 - Wrong Answer 100% (Javascript)

Asked

Viewed 1,401 times

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?

Print do console

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?

  • Show a print of your output

  • I edited the post with this information.

  • You tested the program locally?

1 answer

-1

Probably by the statement, he just wants integer values. The way you’re doing, for example: you get the valor = 50, and when divided by all notes, you divide by 100 too, which gives an output of 0.5 nota(s) de R$ 100, it would be right not to use that note.

Edit: remove also the parseInt(), since you are not converting a string to an integer. If you want to round, use Math methods.

MDN documentation on parseInt(): https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt

  • And the parseInt as a result of the division?

  • Exactly! I used parseint on the console.log.

  • @Woss had not seen, I added in the reply.

  • But that’s not the problem. The documentation itself you quoted says that if the parameter is not a string it is converted to such before returning the integer, this way it is possible to get the whole part of a float with this function.

  • If you want to test, take the first two lines of code and do const valor = 576.73. The output is exactly as expected in the exercise.

  • I get it, but in this case, can the broker himself identify this and reset the answer, no? Why are you using a parseint() where it is not necessary.

  • Exactly, @Woss! I printed out exactly what you said in the statement.

  • @I don’t understand where you’re going with this. Try this on the console here: console.log(10/3) console.log(parseint(10/3)) The result of the second console is exactly as expected.

  • @Anacarolinahernandes I understood this part, I am speaking the following, the result is right? Yes, it is. But is it implemented in the right way? Who fixes this? Is it an automatic fix or a human being who is saying it’s wrong? Because someone can see your code, see using a parseInt() on something that is not a string, even javascript converting automatically, is not "right" way to solve the problem, if you are dealing with numbers, the most "correct" would be to use the Math. And even an automatic correction can check such things.

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.