Skip to content

Instantly share code, notes, and snippets.

@ZackStone
Last active December 18, 2023 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ZackStone/241146636aab47625a97658791e6dd6f to your computer and use it in GitHub Desktop.
Save ZackStone/241146636aab47625a97658791e6dd6f to your computer and use it in GitHub Desktop.
Calculadora - Só pode somar
var calc = {};
calc.somar = function (x,y) {
//console.log(`somando ${x} com ${y}`);
return x + y;
}
calc.multiplicar = function(x ,y) {
var resultado = 0;
for (var i = 0; i < y; i++) {
resultado = calc.somar(resultado, x);
}
return resultado;
}
calc.subtrair = function (x,y) {
return calc.somar(x, -y);
}
calc.dividir = function(x, y) {
var resultado = 0;
while (x >= y) {
resultado = calc.somar(resultado, 1);
x = calc.subtrair(x, y);
}
return resultado;
}
calc.somarLista = function(arr) {
var resultado = 0;
for (var i = 0; i < arr.length; i++) {
resultado = calc.somar(resultado, arr[i]);
}
return resultado;
}
calc.calcularMedia = function(arr) {
var resultado = calc.somarLista(arr);
return calc.dividir(resultado, arr.length);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment