Given 2 values, using a single line of code swap their values and return an array of the swapped values.
Tasks
- Optimize the current solution
- Write more robust tests
- Write a more robust description
Example Starting Setup
// This code is intentionally written without {} so your solution is only a single line
const swapValues = (a, b) => (/* your code here */);
// This code is intentionally written without {} so your solution is only a single line
const swapValues = (a, b) => ([a, b] = [b, a]);
const { strictEqual, deepEqual } = require("chai").assert;
describe("Basic Tests", function () {
function doTest(n1, n2, expected) {
const actual = swapValues(n1, n2);
deepEqual(actual, expected);
}
it("should be a function", function () {
strictEqual(typeof swapValues, "function");
});
it("should return an array", function () {
strictEqual(Array.isArray(swapValues(1, 2)), true);
});
it("should return the correct array", function () {
doTest(0, 5, [5, 0]);
doTest("Hello", "World", ["World", "Hello"]);
doTest(true, false, [false, true]);
doTest(null, undefined, [undefined, null]);
const [a, b] = [[0, 1], [1, 0]];
const res = [b, a];
doTest(a, b, res);
});
});
Given 2 whole numbers (low
& high
), write a function to find all whole numbers between the given values as an array. Return that array in reverse order.
Tasks
- Optimize the current solution
- Create a solution that makes the
EXTRA TESTS
pass (string inputs, high to low) - Write more robust tests
- Write a more robust description
Example Starting Setup
const reverseOrder = (low, high) => {
// your code here
};
const reverseOrder = (low, high) => {
let reverseArray = [];
for (let i = high; i > low - 1; i--) {
reverseArray.push(i);
}
return reverseArray;
};
const { strictEqual, deepEqual } = require("chai").assert;
describe("Basic Tests", function () {
function doTest(n1, n2, expected) {
const actual = reverseOrder(n1, n2);
deepEqual(actual, expected);
}
it("should be a function", function () {
strictEqual(typeof reverseOrder, "function");
});
it("should return an array", function () {
strictEqual(Array.isArray(reverseOrder(1, 2)), true);
});
it("should return the correct array", function () {
doTest(0, 5, [5, 4, 3, 2, 1, 0]);
doTest(5, 7, [7, 6, 5]);
doTest(1, 1, [1]);
doTest(-3, 2, [2, 1, 0, -1, -2, -3]);
});
// it("EXTRA TESTS", function () {
// // return numbers if given string
// doTest("5", "7", [7, 6, 5]);
// // reverse array if high and low are switched
// doTest(5, 4, [4, 5]);
// });
});
// def multiply (a,b): // return a * b const multiply = (a, b) => a * b;
// # def multiply (a,b):// # return a * b- // def multiply (a,b):
- // return a * b
multiply = (a, b) => a * b;- const multiply = (a, b) => a * b;
const { strictEqual } = require("chai").assert; describe("Basic Tests", function () { function doTest(num1, num2, expected) { const actual = multiply(num1, num2); strictEqual(actual, expected, `For ${num1} & ${num2}\n`); } it("should multiply correctly", function () { doTest(0, 5, 0); doTest(1, 5, 5); doTest(2, 5, 10); doTest(-2, 5, -10); }); });
// 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");- const { strictEqual } = require("chai").assert;
describe("Solution", function() {it("should test for something", function() {// Test.assertEquals(1 + 1, 2);// assert.strictEqual(1 + 1, 2);- describe("Basic Tests", function () {
- function doTest(num1, num2, expected) {
- const actual = multiply(num1, num2);
- strictEqual(actual, expected, `For ${num1} & ${num2}\n`);
- }
- it("should multiply correctly", function () {
- doTest(0, 5, 0);
- doTest(1, 5, 5);
- doTest(2, 5, 10);
- doTest(-2, 5, -10);
- });
- });
Checks for binary truthy and falsey parameters
const { assert } = require("chai"); describe("Solution", function () { it("should test for something", function () { assert.strictEqual(helloWorld(true), `Hello World baby`); assert.strictEqual(helloWorld(2 + 2 == 5), `No World`); assert.strictEqual(helloWorld(1), `Hello World baby`); assert.strictEqual(helloWorld(0), `No World`); }); });
const { assert } = require('chai');- const { assert } = require("chai");
describe("Solution", function() {it("should test for something", function() {- describe("Solution", function () {
- it("should test for something", function () {
- assert.strictEqual(helloWorld(true), `Hello World baby`);
assert.strictEqual(helloWorld(2 + 2 == 5), `No World`);- assert.strictEqual(helloWorld(2 + 2 == 5), `No World`);
- assert.strictEqual(helloWorld(1), `Hello World baby`);
- assert.strictEqual(helloWorld(0), `No World`);
- });
- });