Insure that both arguments are number. For example sum("1", 1) returns "11"
Implement a function, which gets string, splits it onto numbers and words. After it wraps numbers in div
tag, words in span
and remove spaces at the end and begining.
Example:
- transformText("Two 2"):<span>Two</span><div>2</div>
- transformText('12 people in 2 rooms'): <div>12</div><span>people in</span><div>2</div><span>rooms</span>
function transformText(string) {
const regExp = /[\D]+|([\d+\.]+)/g
const numsRegExp = /[0-9\.]+/g
const splited = string.match(regExp)
const res = splited.map((item) => {
if (numsRegExp.test(item)) {
return `<div>${item}</div>`
} else {
return `<span>${item.replace(/^\s|\s$/g, "")}</span>`
}
})
return res.join("")
}
// 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("Example", function() {
// Test.assertEquals(1 + 1, 2);
// assert.strictEqual(1 + 1, 2);
assert.strictEqual(transformText('Number 2'), '<span>Number</span><div>2</div>')
assert.strictEqual(transformText('12 people in 2 rooms'), '<div>12</div><span>people in</span><div>2</div><span>rooms</span>')
assert.strictEqual(transformText('from 2 to 9'), '<span>from</span><div>2</div><span>to</span><div>9</div>')
});
});
const findMax = arr => Math.max(...arr);
const findMax = arr => arr.reduce((a, c) => c > a ? c : a, -Infinity);- const findMax = arr => Math.max(...arr);
const chai = require("chai"); const assert = chai.assert; describe("Find max number in array", function() { it("finds the max number in array",function() { assert.strictEqual(findMax([1, 2, 3, 4, 5]), 5) assert.strictEqual(findMax([1, 2, 3, -4, 5]), 5) assert.strictEqual(findMax([-1, -2, -3, -4, -5]), -1) assert.strictEqual(findMax([1, 2, Infinity]), Infinity) assert.strictEqual(findMax([0, 5, -Infinity]), 5) }) })
- const chai = require("chai");
- const assert = chai.assert;
- describe("Find max number in array", function() {
- it("finds the max number in array",function() {
- assert.strictEqual(findMax([1, 2, 3, 4, 5]), 5)
- assert.strictEqual(findMax([1, 2, 3, -4, 5]), 5)
- assert.strictEqual(findMax([-1, -2, -3, -4, -5]), -1)
- assert.strictEqual(findMax([1, 2, Infinity]), Infinity)
- assert.strictEqual(findMax([0, 5, -Infinity]), 5)
- })
- })