EqualExperts JSFoo Challenge
// source https://jsbin.com/hitipaq
/*
Write a function which reverses the passed string
*/
"use strict";
var reverseString = function reverseString(str) {
if (str) {
return str.split("").reverse().join("");
}
return "";
};
/*
Write a function which removes the passed delimiter from the string.
e.g. 'a-b-c'.removeDelimiter('-') should return 'abc'
*/
String.prototype.removeDelimiter = function (delimiter) {
return this.split(delimiter).join("");
};
/*
Write a function which return the multiplication of three numbers.
The function can be called in any of the following forms:
multiply(2, 3, 4) => 24
multiply(2, 3)(4) => 24
multiply(2)(3, 4) => 24
multiply(2)(3)(4) => 24
*/
function curry(fn) {
var arity = fn.length;
return function fn1() {
var args = Array.prototype.slice.call(arguments, 0);
if (args.length >= arity) {
return fn.apply(null, args);
} else {
return function fn2() {
var args2 = Array.prototype.slice.call(arguments, 0);
return fn1.apply(null, args.concat(args2));
};
}
};
}
var product = function product(a, b, c) {
return a * b * c;
};
var multiply = curry(product);
/*
write a function to check if the two object passed are equal or not.
the objects can be nested.
*/
var areEqualObjs = function areEqualObjs(obj1, obj2) {
if (typeof obj1 !== typeof obj2) {
return false;
}
if (typeof obj1 === "function") {
return obj1.toString() === obj2.toString();
}
if (obj1 instanceof Object && obj2 instanceof Object) {
if (Object.keys(obj1).length !== Object.keys(obj2).length) {
return false;
}
for (k in obj1) {
if (!areEqualObjs(obj1[k], obj2[k])) {
return false;
}
}
return true;
} else {
return obj1 === obj2;
}
};
/*
write a function to faltten the array
*/
Array.prototype.flatten = function () {
var arr = [];
for (var i = 0, l = this.length; i < l; i++) {
if (Array.isArray(this[i])) {
arr = arr.concat(this[i].flatten());
} else {
arr.push(this[i]);
}
}
return arr;
};
//Tests
describe('reverseString', function () {
it('reverses the passed string', function () {
expect(reverseString('')).toBe('');
expect(reverseString('abcde')).toBe('edcba');
});
});
describe('removeDelimiter', function () {
it('removes the passed delimiter from the string', function () {
expect(reverseString('')).toBe('');
expect("Equal-Experts".removeDelimiter('-')).toEqual('EqualExperts');
});
});
describe('multiply', function () {
it('curries the multiply for three arguments', function () {
expect(multiply(2, 3, 4)).toBe(24);
expect(multiply(2, 3)(4)).toBe(24);
expect(multiply(2)(3, 4)).toBe(24);
expect(multiply(2)(3)(4)).toBe(24);
});
});
describe('areEqualObjs', function () {
it('compares the objects', function () {
expect(areEqualObjs({
a: 1,
b: 2
}, {
a: 1,
b: 2
})).toBeTruthy();
expect(areEqualObjs({
a: 1,
b: 2
}, {
a: 1,
b: 3
})).toBeFalsy();
expect(areEqualObjs({
a: 1,
b: 2
}, {
a: 1
})).toBeFalsy();
expect(areEqualObjs({
a: {
b: 1
}
}, {
a: {
b: 1
}
})).toBeTruthy();
expect(areEqualObjs({
a: {
b: 1
}
}, {
a: {
b: 2
}
})).toBeFalsy();
expect(areEqualObjs({
a: {
b: 1,
c: {
d: 2
}
}
}, {
a: {
b: 1,
c: {
d: 3
}
}
})).toBeFalsy();
expect(areEqualObjs({
a: {
b: 1,
c: {
d: {
e: 2
}
}
}
}, {
a: {
b: 1,
c: {
d: {
e: 2
}
}
}
})).toBeTruthy();
});
});
describe('flattenArray', function () {
it('flattens the passed array', function () {
expect([].flatten()).toEqual([]);
expect([1, 2].flatten()).toEqual([1, 2]);
expect([1, [2]].flatten()).toEqual([1, 2]);
expect([1, [2, 3]].flatten()).toEqual([1, 2, 3]);
expect([[1], [2], [3], [4], [5]].flatten()).toEqual([1, 2, 3, 4, 5]);
expect([1, 2, [3, [4, 5, 6]], [7, 8]].flatten()).toEqual([1, 2, 3, 4, 5, 6, 7, 8]);
expect([1, [2, [3, [4, [5, [6, [7, [8]]]]]]]].flatten()).toEqual([1, 2, 3, 4, 5, 6, 7, 8]);
});
});
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>EqualExperts JSFoo Challenge</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.3.4/jasmine.min.css">
<script src="http://cdnjs.cloudflare.com/ajax/libs/jasmine/2.3.4/jasmine.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/jasmine/2.3.4/jasmine-html.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/jasmine/2.3.4/boot.min.js"></script>
</head>
<body>
<script id="jsbin-javascript">
/*
Write a function which reverses the passed string
*/
"use strict";
var reverseString = function reverseString(str) {
if (str) {
return str.split("").reverse().join("");
}
return "";
};
/*
Write a function which removes the passed delimiter from the string.
e.g. 'a-b-c'.removeDelimiter('-') should return 'abc'
*/
String.prototype.removeDelimiter = function (delimiter) {
return this.split(delimiter).join("");
};
/*
Write a function which return the multiplication of three numbers.
The function can be called in any of the following forms:
multiply(2, 3, 4) => 24
multiply(2, 3)(4) => 24
multiply(2)(3, 4) => 24
multiply(2)(3)(4) => 24
*/
function curry(fn) {
var arity = fn.length;
return function fn1() {
var args = Array.prototype.slice.call(arguments, 0);
if (args.length >= arity) {
return fn.apply(null, args);
} else {
return function fn2() {
var args2 = Array.prototype.slice.call(arguments, 0);
return fn1.apply(null, args.concat(args2));
};
}
};
}
var product = function product(a, b, c) {
return a * b * c;
};
var multiply = curry(product);
/*
write a function to check if the two object passed are equal or not.
the objects can be nested.
*/
var areEqualObjs = function areEqualObjs(obj1, obj2) {
if (typeof obj1 !== typeof obj2) {
return false;
}
if (typeof obj1 === "function") {
return obj1.toString() === obj2.toString();
}
if (obj1 instanceof Object && obj2 instanceof Object) {
if (Object.keys(obj1).length !== Object.keys(obj2).length) {
return false;
}
for (k in obj1) {
if (!areEqualObjs(obj1[k], obj2[k])) {
return false;
}
}
return true;
} else {
return obj1 === obj2;
}
};
/*
write a function to faltten the array
*/
Array.prototype.flatten = function () {
var arr = [];
for (var i = 0, l = this.length; i < l; i++) {
if (Array.isArray(this[i])) {
arr = arr.concat(this[i].flatten());
} else {
arr.push(this[i]);
}
}
return arr;
};
//Tests
describe('reverseString', function () {
it('reverses the passed string', function () {
expect(reverseString('')).toBe('');
expect(reverseString('abcde')).toBe('edcba');
});
});
describe('removeDelimiter', function () {
it('removes the passed delimiter from the string', function () {
expect(reverseString('')).toBe('');
expect("Equal-Experts".removeDelimiter('-')).toEqual('EqualExperts');
});
});
describe('multiply', function () {
it('curries the multiply for three arguments', function () {
expect(multiply(2, 3, 4)).toBe(24);
expect(multiply(2, 3)(4)).toBe(24);
expect(multiply(2)(3, 4)).toBe(24);
expect(multiply(2)(3)(4)).toBe(24);
});
});
describe('areEqualObjs', function () {
it('compares the objects', function () {
expect(areEqualObjs({
a: 1,
b: 2
}, {
a: 1,
b: 2
})).toBeTruthy();
expect(areEqualObjs({
a: 1,
b: 2
}, {
a: 1,
b: 3
})).toBeFalsy();
expect(areEqualObjs({
a: 1,
b: 2
}, {
a: 1
})).toBeFalsy();
expect(areEqualObjs({
a: {
b: 1
}
}, {
a: {
b: 1
}
})).toBeTruthy();
expect(areEqualObjs({
a: {
b: 1
}
}, {
a: {
b: 2
}
})).toBeFalsy();
expect(areEqualObjs({
a: {
b: 1,
c: {
d: 2
}
}
}, {
a: {
b: 1,
c: {
d: 3
}
}
})).toBeFalsy();
expect(areEqualObjs({
a: {
b: 1,
c: {
d: {
e: 2
}
}
}
}, {
a: {
b: 1,
c: {
d: {
e: 2
}
}
}
})).toBeTruthy();
});
});
describe('flattenArray', function () {
it('flattens the passed array', function () {
expect([].flatten()).toEqual([]);
expect([1, 2].flatten()).toEqual([1, 2]);
expect([1, [2]].flatten()).toEqual([1, 2]);
expect([1, [2, 3]].flatten()).toEqual([1, 2, 3]);
expect([[1], [2], [3], [4], [5]].flatten()).toEqual([1, 2, 3, 4, 5]);
expect([1, 2, [3, [4, 5, 6]], [7, 8]].flatten()).toEqual([1, 2, 3, 4, 5, 6, 7, 8]);
expect([1, [2, [3, [4, [5, [6, [7, [8]]]]]]]].flatten()).toEqual([1, 2, 3, 4, 5, 6, 7, 8]);
});
});
</script>
<script id="jsbin-source-html" type="text/html"><!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>EqualExperts JSFoo Challenge</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.3.4/jasmine.min.css">
<script src="//cdnjs.cloudflare.com/ajax/libs/jasmine/2.3.4/jasmine.min.js"><\/script>
<script src="//cdnjs.cloudflare.com/ajax/libs/jasmine/2.3.4/jasmine-html.min.js"><\/script>
<script src="//cdnjs.cloudflare.com/ajax/libs/jasmine/2.3.4/boot.min.js"><\/script>
</head>
<body>
</body>
</html></script>
<script id="jsbin-source-javascript" type="text/javascript">
/*
Write a function which reverses the passed string
*/
const reverseString = (str) => {
if(str){
return str.split("").reverse().join("");
}
return "";
}
/*
Write a function which removes the passed delimiter from the string.
e.g. 'a-b-c'.removeDelimiter('-') should return 'abc'
*/
String.prototype.removeDelimiter = function(delimiter) {
return this.split(delimiter).join("");
};
/*
Write a function which return the multiplication of three numbers.
The function can be called in any of the following forms:
multiply(2, 3, 4) => 24
multiply(2, 3)(4) => 24
multiply(2)(3, 4) => 24
multiply(2)(3)(4) => 24
*/
function curry(fn) {
var arity = fn.length;
return function fn1() {
var args = Array.prototype.slice.call(arguments, 0);
if (args.length >= arity) {
return fn.apply(null, args);
}
else {
return function fn2() {
var args2 = Array.prototype.slice.call(arguments, 0);
return fn1.apply(null, args.concat(args2));
}
}
};
}
const product = (a, b, c) => a * b * c;
const multiply = curry(product);
/*
write a function to check if the two object passed are equal or not.
the objects can be nested.
*/
const areEqualObjs = (obj1, obj2) => {
if (typeof(obj1) !== typeof(obj2)) {
return false;
}
if (typeof(obj1) === "function") {
return obj1.toString() === obj2.toString();
}
if (obj1 instanceof Object && obj2 instanceof Object) {
if (Object.keys(obj1).length !== Object.keys(obj2).length) {
return false;
}
for (k in obj1) {
if (!areEqualObjs(obj1[k], obj2[k])) {
return false;
}
}
return true;
} else {
return obj1 === obj2;
}
}
/*
write a function to faltten the array
*/
Array.prototype.flatten = function() {
let arr = [];
for(let i = 0, l = this.length; i < l; i++) {
if(Array.isArray(this[i])) {
arr = arr.concat(this[i].flatten());
} else {
arr.push(this[i]);
}
}
return arr;
};
//Tests
describe('reverseString', () => {
it('reverses the passed string', () => {
expect(reverseString('')).toBe('');
expect(reverseString('abcde')).toBe('edcba');
});
});
describe('removeDelimiter', () => {
it('removes the passed delimiter from the string', () => {
expect(reverseString('')).toBe('');
expect("Equal-Experts".removeDelimiter('-')).toEqual('EqualExperts');
});
});
describe('multiply', () => {
it('curries the multiply for three arguments', () => {
expect(multiply(2, 3, 4)).toBe(24);
expect(multiply(2, 3)(4)).toBe(24);
expect(multiply(2)(3, 4)).toBe(24);
expect(multiply(2)(3)(4)).toBe(24);
});
});
describe('areEqualObjs', () => {
it('compares the objects', () => {
expect(areEqualObjs({
a: 1,
b: 2
}, {
a: 1,
b: 2
})).toBeTruthy();
expect(areEqualObjs({
a: 1,
b: 2
}, {
a: 1,
b: 3
})).toBeFalsy();
expect(areEqualObjs({
a: 1,
b: 2
}, {
a: 1
})).toBeFalsy();
expect(areEqualObjs({
a: {
b: 1
}
}, {
a: {
b: 1
}
})).toBeTruthy();
expect(areEqualObjs({
a: {
b: 1
}
}, {
a: {
b: 2
}
})).toBeFalsy();
expect(areEqualObjs({
a: {
b: 1,
c: {
d: 2
}
}
}, {
a: {
b: 1,
c: {
d: 3
}
}
})).toBeFalsy();
expect(areEqualObjs({
a: {
b: 1,
c: {
d: {
e: 2
}
}
}
}, {
a: {
b: 1,
c: {
d: {
e: 2
}
}
}
})).toBeTruthy();
})
});
describe('flattenArray',() => {
it('flattens the passed array', () => {
expect([].flatten()).toEqual([]);
expect([1, 2].flatten()).toEqual([1, 2]);
expect([1, [2]].flatten()).toEqual([1, 2]);
expect([1, [2, 3]].flatten()).toEqual([1, 2, 3]);
expect([[1], [2], [3], [4], [5]].flatten()).toEqual([1, 2, 3, 4, 5]);
expect([1, 2, [3, [4, 5, 6]], [7, 8]].flatten()).toEqual([1, 2, 3, 4, 5, 6, 7, 8]);
expect([1, [2, [3, [4, [5, [6, [7, [8]]]]]]]].flatten()).toEqual([1, 2, 3, 4, 5, 6, 7, 8]);
});
});
</script></body>
</html>