Joe Hill
Joe Hill

Reputation: 93

Why I keep getting wrong answer in this problem?

I need help with this logic problem in Uri Online Judge site:

The submitted code starts in "var n", I used the var lines to work with an example of input:

var lines = ["400.00"]
/**
 * Code your solution here
 */
var n = parseFloat(lines[0]);

if (n <= 400.0) {
    console.log("Novo salario: " + Math.round(n * 1.15).toFixed(2));
    console.log("Reajuste ganho: " + (n * 0.15).toFixed(2)); 
    console.log("Em percentual: 15 %");
} else if (n <= 800.0) {
    console.log("Novo salario: " + Math.round(n * 1.12).toFixed(2));
    console.log("Reajuste ganho: " + (n * 0.12).toFixed(2)); 
    console.log("Em percentual: 12 %");
} else if (n <= 1200.0) {
    console.log("Novo salario: " + Math.round(n * 1.10).toFixed(2));
    console.log("Reajuste ganho: " + (n * 0.10).toFixed(2)); 
    console.log("Em percentual: 10 %");
} else if (n <= 2000.0) {
    console.log("Novo salario: " + Math.round(n * 1.07).toFixed(2));
    console.log("Reajuste ganho: " + (n * 0.07).toFixed(2)); 
    console.log("Em percentual: 7 %");
} else {
    console.log("Novo salario: " + Math.round(n * 1.04).toFixed(2));
    console.log("Reajuste ganho: " + (n * 0.04).toFixed(2)); 
    console.log("Em percentual: 4 %");
}

when I submit this: the console shows: Wrong answer (5%)

Upvotes: 2

Views: 213

Answers (1)

Avin Kavish
Avin Kavish

Reputation: 8937

The first problem I see is you using Math.round() when the question does not mention any sort of rounding anywhere.

Math.round(n * 1.12).toFixed(2)

The example output shows 880.01 which is unobtainable with Math.round as it rounds to the nearest integer.

Edit: Here's concise way to write this without repeat yourself.

let factor; <-- // var factor; if no es6
if (n <= 400.0)
  factor = 0.15
else if (n <= 800.0)
  factor = 0.12
else if (n <= 1200.0)
  factor = 0.1
else if (n <= 2000.0)
  factor = 0.07
else
  factor = 0.04


console.log("Novo salario: " + (n * (1 + factor)).toFixed(2));
console.log("Reajuste ganho: " + (n * factor).toFixed(2)); 
console.log("Em percentual: " + factor * 100 + " %");

Upvotes: 7

Related Questions