提交的代码以“ var n”开头,我使用var行来处理输入示例:
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 %");
}
答案 0 :(得分:7)
我看到的第一个问题是您使用Math.round()
时,该问题未提及任何舍入形式。
Math.round(n * 1.12).toFixed(2)
示例输出显示880.01
,因为Math.round
舍入到最接近的整数是无法获得的。
编辑:这是简洁的编写方式,无需重复。
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 + " %");