function calculator(num1, num2, operationString) { return eval(num1 + operationString + num2) } calculator(1, 2, "+");
- function calculator(num1, num2, operationString) {
if (operationString == "-") {return num1 - num2;}if (operationString == "+") {return num1 + num2;}if (operationString == "*") {return num1 * num2;}if (operationString == "/") {return num1 / num2;}- return eval(num1 + operationString + num2)
- }
- calculator(1, 2, "+");
const chai = require("chai"); const assert = chai.assert; chai.config.truncateThreshold = 0; describe('+', () => { for(let i = 0; i < 10; i++) { const a = Math.random() * 100, b = Math.random() * 100; const exp = a + b; it(`${a} + ${b} = ${exp}`, () => { assert.deepEqual(calculator(a, b, '+'), exp) }); } }); describe('-', () => { for(let i = 0; i < 10; i++) { const a = Math.random() * 100, b = Math.random() * 100; const exp = a - b; it(`${a} - ${b} = ${exp}`, () => { assert.deepEqual(calculator(a, b, '-'), exp) }); } }); describe('*', () => { for(let i = 0; i < 10; i++) { const a = Math.random() * 100, b = Math.random() * 100; const exp = a * b; it(`${a} * ${b} = ${exp}`, () => { assert.deepEqual(calculator(a, b, '*'), exp) }); } }); describe('/', () => { for(let i = 0; i < 10; i++) { const a = Math.random() * 100, b = Math.random() * 100; const exp = a / b; it(`${a} / ${b} = ${exp}`, () => { assert.deepEqual(calculator(a, b, '/'), exp) }); } });
//- const chai = require("chai");
- const assert = chai.assert;
- chai.config.truncateThreshold = 0;
- describe('+', () => {
- for(let i = 0; i < 10; i++) {
- const a = Math.random() * 100, b = Math.random() * 100;
- const exp = a + b;
- it(`${a} + ${b} = ${exp}`, () => {
- assert.deepEqual(calculator(a, b, '+'), exp)
- });
- }
- });
- describe('-', () => {
- for(let i = 0; i < 10; i++) {
- const a = Math.random() * 100, b = Math.random() * 100;
- const exp = a - b;
- it(`${a} - ${b} = ${exp}`, () => {
- assert.deepEqual(calculator(a, b, '-'), exp)
- });
- }
- });
- describe('*', () => {
- for(let i = 0; i < 10; i++) {
- const a = Math.random() * 100, b = Math.random() * 100;
- const exp = a * b;
- it(`${a} * ${b} = ${exp}`, () => {
- assert.deepEqual(calculator(a, b, '*'), exp)
- });
- }
- });
- describe('/', () => {
- for(let i = 0; i < 10; i++) {
- const a = Math.random() * 100, b = Math.random() * 100;
- const exp = a / b;
- it(`${a} / ${b} = ${exp}`, () => {
- assert.deepEqual(calculator(a, b, '/'), exp)
- });
- }
- });