The problem is that the prompt()
returns a string.
This is not noticeable in multiplication because Javascript tries to convert into numbers but when you start this concatenation with a string, the conversion is left to right and everything is left string.
Forehead typeof cFabrica
and you’ll see that it does string
.
The solution is to convert in number so there is no doubt/bugs.
var cFabrica = prompt("Insira o valor de fabrica do veículo");
// teste 1
console.log('cFabrica é do tipo:', typeof cFabrica);
var numerocFabrica = Number(cFabrica);
// teste 2
console.log('numerocFabrica é do tipo:', typeof numerocFabrica);
var comissao = 0.28 * cFabrica;
var imposto = 0.45 * cFabrica;
var cFinal = numerocFabrica + comissao + imposto;
console.log("O valor final é: ", cFinal);
As a suggestion, study javascript typing and operations. Since the language is not strongly typed, errors like this are common.
– Brunno Vianna