Created
May 26, 2017 14:36
-
-
Save rflpazini/6e20244403ea3b305c8fd87241851434 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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