The task is reaaaaly easy,you're given an array of different numbers, return the sum of even numbers.If there is no even numbers or there is empty array,return false.
function SumEven(arr){
let sum=0;
for(let i=0;i<arr.length;i++){
if(arr[i]%2===0) sum+=arr[i];
}
if(sum===0 || arr.length===0)
return false
else
return sum;
}
// 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("Give a try", function() {
assert.strictEqual(SumEven([]),false);
assert.strictEqual(SumEven([1,2,3,4,5,6]),12);
assert.strictEqual(SumEven([91,22,33]),22);
assert.strictEqual(SumEven([8,10,24,1,0]),42);
assert.strictEqual(SumEven([75,19,101,309,703]),false);
});
});
You are given an array containing characters, numbers, and null values. The task is to return a new array where the elements are ordered as follows: null values first, followed by numbers (sorted in ascending order), and characters (sorted in alphabetical order). All elements should be sorted within their respective categories.
function falsyNumberChar(arr) {
let falsy = [],
number = [],
char = [];
arr.forEach((element) => {
if (element === null) falsy.push(element);
else if (Number(element)) number.push(element);
else if (String(element)) char.push(element);
});
number.sort();
char.sort();
return [...falsy, ...number, ...char];
}
// 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("Let's test it", function() {
assert.deepStrictEqual(falsyNumberChar([5, null, "b", 3, 7, "h", 6]), [null, 3, 5, 6, 7, 'b', 'h']);
});
it("Without null", function() {
assert.deepStrictEqual(falsyNumberChar(["a", "u", 6, "p", 534, "k"]), [534, 6, 'a', 'k', 'p', 'u']);
});
it("Without number", function() {
assert.deepStrictEqual(falsyNumberChar(["b", "q", "b", "v", null, null]),[null, null, 'b', 'b', 'q', 'v']);
});
it("Without Char", function() {
assert.deepStrictEqual(falsyNumberChar([9, 2, 12125, 5, null, 120,5,96,35,12]), [null, 12, 120, 12125, 2, 35, 5, 5, 9, 96]);
});
});