Mathematics
A simple calculator, with an identifier string "calc", x, and y.
Make sure to add the right strings, with 3 letters each.
function calculator(calc,x,y){
if(!calc||!x||!y) return false;
if(calc == "add") return x+y;
if(calc == "sub") return x-y;
if(calc == "tms") return x*y;
if(calc == "div") return x/y;
if(calc == "pwr") return x**y;
return false;
}
const chai = require("chai");
const assert = chai.assert;
describe("Solution", function() {
it("should test for something", function() {
assert.strictEqual(calculator("add",1,1), 2);
assert.strictEqual(calculator("mlt",1,1), false);
assert.strictEqual(calculator("pwr",2,3), 8);
});
});
Fundamentals
Mathematics
Tutorials
Simple, but it might be good for the basics.
Hint: 'Number's are good for 'Math'.
function isSquare(n){
return Number.isInteger(Math.sqrt(n));
}
// Since Node 10, we're using Mocha.
// You can use `chai` for assertions.
const chai = require("chai");
const assert = chai.assert;
// Uncomment the following line to disable truncating failure messages for deep equals, do:
// chai.config.truncateThreshold = 0;
// Since Node 12, we no longer include assertions from our deprecated custom test framework by default.
// Uncomment the following to use the old assertions:
const Test = require("@codewars/test-compat");
describe("Solution", function() {
it("should test for something", function() {
Test.assertEquals(isSquare(7), false);
Test.assertEquals(isSquare(9), true);
});
});
Fundamentals
Mathematics
function isDivisible(a,n){
return a%n == 0;
}
// Since Node 10, we're using Mocha.
// You can use `chai` for assertions.
const chai = require("chai");
const assert = chai.assert;
// Uncomment the following line to disable truncating failure messages for deep equals, do:
// chai.config.truncateThreshold = 0;
// Since Node 12, we no longer include assertions from our deprecated custom test framework by default.
// Uncomment the following to use the old assertions:
// const Test = require("@codewars/test-compat");
describe("Solution", function() {
it("should test for something", function() {
assert.strictEqual(isDivisible(6, 2), true);
// assert.strictEqual(1 + 1, 2);
});
});