Skip to content

Instantly share code, notes, and snippets.

@rflpazini
Created May 26, 2017 14:36
Show Gist options
  • Save rflpazini/6e20244403ea3b305c8fd87241851434 to your computer and use it in GitHub Desktop.
Save rflpazini/6e20244403ea3b305c8fd87241851434 to your computer and use it in GitHub Desktop.
class Calculadora {
soma(a, b) {
let result;
// Verifica se os tipos dos arguentos que recebemos
// são do tipo 'number'. Caso não forem, iremos retornar um erro
if (typeof a != 'number' || typeof b != 'number') {
throw new Error("Os valores devem ser apenas números");
}
result = a + b;
// Verifica se o resultado da soma convertido em inteiro não é igual ao resultado
// se isso for verdade, convertemos o número para float e limitamos o mesmo
// para apenas duas casas depois da vírgula
if (parseInt(result) != result) {
result = parseFloat(result.toFixed(2));
}
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment