diff --git a/08_calculator/calculator-events.js b/08_calculator/calculator-events.js new file mode 100644 index 0000000..68fb550 --- /dev/null +++ b/08_calculator/calculator-events.js @@ -0,0 +1,29 @@ +const display = document.getElementById('display'); +let currentInput = ''; + +document.querySelectorAll('.btn').forEach(button => { + button.addEventListener('click', () => handleButtonClick(button.innerText)); +}); + +function handleButtonClick(value) { + switch (value) { + case 'C': + currentInput = ''; + break; + case '←': + currentInput = currentInput.slice(0, -1); + break; + case '=': + try { + currentInput = eval(currentInput).toString(); + } catch (error) { + currentInput = 'Error'; + } + break; + default: + currentInput += value; + break; + } + + display.innerText = currentInput; +} diff --git a/08_calculator/calculator.js b/08_calculator/calculator.js index c22e8d2..ed2227e 100644 --- a/08_calculator/calculator.js +++ b/08_calculator/calculator.js @@ -1,33 +1,27 @@ -const add = function() { - +const calculator = { + add: function (a, b) { + return a + b; + }, + subtract: function (a, b) { + return a - b; + }, + sum: function (array) { + return array.reduce((total, current) => total + current, 0); + }, + multiply: function (...args) { + return args.reduce((product, current) => product * current, 1); + }, + power: function (a, b) { + return Math.pow(a, b); + }, + factorial: function (n) { + if (n === 0) return 1; + let product = 1; + for (let i = n; i > 0; i--) { + product *= i; + } + return product; + }, }; -const subtract = function() { - -}; - -const sum = function() { - -}; - -const multiply = function() { - -}; - -const power = function() { - -}; - -const factorial = function() { - -}; - -// Do not edit below this line -module.exports = { - add, - subtract, - sum, - multiply, - power, - factorial -}; +module.exports = calculator; diff --git a/08_calculator/calculator.test.js b/08_calculator/calculator.test.js new file mode 100644 index 0000000..90abf60 --- /dev/null +++ b/08_calculator/calculator.test.js @@ -0,0 +1,77 @@ +const calculator = require('./calculator'); // Asegúrate de que la ruta sea correcta + +describe('add', () => { + test('adds 0 and 0', () => { + expect(calculator.add(0, 0)).toBe(0); + }); + + test('adds 2 and 2', () => { + expect(calculator.add(2, 2)).toBe(4); + }); + + test('adds positive numbers', () => { + expect(calculator.add(2, 6)).toBe(8); + }); +}); + +describe('subtract', () => { + test('subtracts numbers', () => { + expect(calculator.subtract(10, 4)).toBe(6); + }); +}); + +describe('sum', () => { + test('computes the sum of an empty array', () => { + expect(calculator.sum([])).toBe(0); + }); + + test('computes the sum of an array of one number', () => { + expect(calculator.sum([7])).toBe(7); + }); + + test('computes the sum of an array of two numbers', () => { + expect(calculator.sum([7, 11])).toBe(18); + }); + + test('computes the sum of an array of many numbers', () => { + expect(calculator.sum([1, 3, 5, 7, 9])).toBe(25); + }); +}); + +describe('multiply', () => { + test('multiplies two numbers', () => { + expect(calculator.multiply(2, 4)).toBe(8); + }); + + test('multiplies several numbers', () => { + expect(calculator.multiply(2, 4, 6, 8, 10, 12, 14)).toBe(645120); + }); +}); + +describe('power', () => { + test('raises one number to the power of another number', () => { + expect(calculator.power(4, 3)).toBe(64); // 4 to the third power is 64 + }); +}); + +describe('factorial', () => { + test('computes the factorial of 0', () => { + expect(calculator.factorial(0)).toBe(1); // 0! = 1 + }); + + test('computes the factorial of 1', () => { + expect(calculator.factorial(1)).toBe(1); + }); + + test('computes the factorial of 2', () => { + expect(calculator.factorial(2)).toBe(2); + }); + + test('computes the factorial of 5', () => { + expect(calculator.factorial(5)).toBe(120); + }); + + test('computes the factorial of 10', () => { + expect(calculator.factorial(10)).toBe(3628800); + }); +}); \ No newline at end of file diff --git a/08_calculator/index.html b/08_calculator/index.html new file mode 100644 index 0000000..38fdf94 --- /dev/null +++ b/08_calculator/index.html @@ -0,0 +1,42 @@ + + +
+ + +