Created
May 28, 2018 22:26
-
-
Save backslash112/f0e579cb78b54c97a9a96759fda6d68d to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/jelifixubi
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<script id="jsbin-javascript"> | |
console.log("-------------------------------------------------"); | |
// question 1 | |
const allEquals = arr => { | |
return arr.every(item => item === arr[0]); | |
}; | |
const isDual = arr => { | |
// check the length of array | |
// if the length is 0 then return true directly | |
// if the length is a odd number then return false directly | |
if (arr.length === 0) { | |
return 1; | |
} else if (arr.length % 2 === 1) { | |
return 0; | |
} | |
const result = arr.map((item, index) => { | |
// console.log(index); | |
if (index % 2 === 0) { | |
return item + arr[index+1]; | |
} else { | |
return null; | |
} | |
}) | |
.filter(item => item !== null); | |
// check result are equals | |
return allEquals(result) ? 1 : 0; | |
}; | |
// question 2 | |
// solution 1: | |
function isWeekend1() { | |
const todayDate = new Date(); | |
const day = todayDate.getDay(); | |
const isWeekend1 = day => day === 0 || day === 6 | |
const isWeekday1 = day => day >= 1 && day <= 5 | |
let week = [day]; | |
const weekday = week.filter(isWeekday1).map(day => "weekday"); | |
const weekend = week.filter(isWeekend1).map(day => "weekdend"); | |
// console.log(weekday.concat(weekend)[0]); | |
return weekday.concat(weekend)[0]; | |
} | |
// solution 2: | |
function isWeekend2() { | |
const todayDate = new Date(); | |
const day = todayDate.getDay(); | |
return isWeekendByDay(day); | |
} | |
function isWeekendByDay(day) { | |
const map = ["weekend", "weekday", "weekday", "weekday", "weekday", "weekday", "weekend"]; | |
return map[day]; | |
} | |
// question 3 | |
const applyCoupon = category => { | |
return coupon => { | |
return item => { | |
if (item.category === category) { | |
let newPrice = item.price * (1 - coupon); | |
item.price = newPrice; | |
} | |
return item; | |
} | |
} | |
}; | |
// unit test | |
var unitTests = {}; | |
// test isDual() | |
unitTests.testIsDual1 = function(method) { | |
return method([]) === 1; | |
}; | |
unitTests.testIsDual2 = function(method) { | |
return method([1, 2, 3]) === 0; | |
}; | |
unitTests.testIsDual3 = function(method) { | |
return method([1, 2, 3, 0]) === 1; | |
}; | |
unitTests.testIsDual4 = function(method) { | |
return method([1, 1, 2, 2]) === 0; | |
}; | |
// test allEquals() | |
unitTests.testAllEquals1 = function(method) { | |
return method([2, 3]) === false; | |
}; | |
unitTests.testAllEquals2 = function(method) { | |
return method([3, 3]) === true; | |
}; | |
// test isWeekend1() | |
unitTests.testIsWeekend1 = function(method) { | |
return method() == "weekday"; | |
} | |
// test isWeekend2() | |
unitTests.testIsWeekend2 = function(method) { | |
return method() == "weekday"; | |
} | |
// test isWeekendByDay() | |
unitTests.testIsWeekendByDay1 = function(method) { | |
// 0 is Sunday | |
return method(0) === "weekend" && method(6) === "weekend"; | |
} | |
unitTests.testIsWeekendByDay2 = function(method) { | |
return method(1) === "weekday" && method(5) === "weekday"; | |
} | |
// test applyCoupon1() | |
unitTests.testApplyCoupon1 = function(method) { | |
const item = { | |
"name": "Biscuits", | |
"type": "regular", | |
"category": "food", | |
"price": 2.0 | |
} | |
return method("food")(0.1)(item).price === 1.8; | |
} | |
console.log("test isDual(): "); | |
console.log(unitTests.testIsDual1(isDual)); | |
console.log(unitTests.testIsDual2(isDual)); | |
console.log(unitTests.testIsDual3(isDual)); | |
console.log("test allEquals(): "); | |
console.log(unitTests.testAllEquals1(allEquals)); | |
console.log(unitTests.testAllEquals2(allEquals)); | |
console.log("test isWeekend1(): "); | |
console.log(unitTests.testIsWeekend1(isWeekend1)); | |
console.log("test isWeekend2(): "); | |
console.log(unitTests.testIsWeekend2(isWeekend2)); | |
console.log("test isWeekendByDay(): "); | |
console.log(unitTests.testIsWeekendByDay1(isWeekendByDay) === true); | |
console.log(unitTests.testIsWeekendByDay2(isWeekendByDay) === true); | |
console.log("test applyCoupon(): "); | |
console.log(unitTests.testApplyCoupon1(applyCoupon)); | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">console.log("-------------------------------------------------"); | |
// question 1 | |
const allEquals = arr => { | |
return arr.every(item => item === arr[0]); | |
}; | |
const isDual = arr => { | |
// check the length of array | |
// if the length is 0 then return true directly | |
// if the length is a odd number then return false directly | |
if (arr.length === 0) { | |
return 1; | |
} else if (arr.length % 2 === 1) { | |
return 0; | |
} | |
const result = arr.map((item, index) => { | |
// console.log(index); | |
if (index % 2 === 0) { | |
return item + arr[index+1]; | |
} else { | |
return null; | |
} | |
}) | |
.filter(item => item !== null); | |
// check result are equals | |
return allEquals(result) ? 1 : 0; | |
}; | |
// question 2 | |
// solution 1: | |
function isWeekend1() { | |
const todayDate = new Date(); | |
const day = todayDate.getDay(); | |
const isWeekend1 = day => day === 0 || day === 6 | |
const isWeekday1 = day => day >= 1 && day <= 5 | |
let week = [day]; | |
const weekday = week.filter(isWeekday1).map(day => "weekday"); | |
const weekend = week.filter(isWeekend1).map(day => "weekdend"); | |
// console.log(weekday.concat(weekend)[0]); | |
return weekday.concat(weekend)[0]; | |
} | |
// solution 2: | |
function isWeekend2() { | |
const todayDate = new Date(); | |
const day = todayDate.getDay(); | |
return isWeekendByDay(day); | |
} | |
function isWeekendByDay(day) { | |
const map = ["weekend", "weekday", "weekday", "weekday", "weekday", "weekday", "weekend"]; | |
return map[day]; | |
} | |
// question 3 | |
const applyCoupon = category => { | |
return coupon => { | |
return item => { | |
if (item.category === category) { | |
let newPrice = item.price * (1 - coupon); | |
item.price = newPrice; | |
} | |
return item; | |
} | |
} | |
}; | |
// unit test | |
var unitTests = {}; | |
// test isDual() | |
unitTests.testIsDual1 = function(method) { | |
return method([]) === 1; | |
}; | |
unitTests.testIsDual2 = function(method) { | |
return method([1, 2, 3]) === 0; | |
}; | |
unitTests.testIsDual3 = function(method) { | |
return method([1, 2, 3, 0]) === 1; | |
}; | |
unitTests.testIsDual4 = function(method) { | |
return method([1, 1, 2, 2]) === 0; | |
}; | |
// test allEquals() | |
unitTests.testAllEquals1 = function(method) { | |
return method([2, 3]) === false; | |
}; | |
unitTests.testAllEquals2 = function(method) { | |
return method([3, 3]) === true; | |
}; | |
// test isWeekend1() | |
unitTests.testIsWeekend1 = function(method) { | |
return method() == "weekday"; | |
} | |
// test isWeekend2() | |
unitTests.testIsWeekend2 = function(method) { | |
return method() == "weekday"; | |
} | |
// test isWeekendByDay() | |
unitTests.testIsWeekendByDay1 = function(method) { | |
// 0 is Sunday | |
return method(0) === "weekend" && method(6) === "weekend"; | |
} | |
unitTests.testIsWeekendByDay2 = function(method) { | |
return method(1) === "weekday" && method(5) === "weekday"; | |
} | |
// test applyCoupon1() | |
unitTests.testApplyCoupon1 = function(method) { | |
const item = { | |
"name": "Biscuits", | |
"type": "regular", | |
"category": "food", | |
"price": 2.0 | |
} | |
return method("food")(0.1)(item).price === 1.8; | |
} | |
console.log("test isDual(): "); | |
console.log(unitTests.testIsDual1(isDual)); | |
console.log(unitTests.testIsDual2(isDual)); | |
console.log(unitTests.testIsDual3(isDual)); | |
console.log("test allEquals(): "); | |
console.log(unitTests.testAllEquals1(allEquals)); | |
console.log(unitTests.testAllEquals2(allEquals)); | |
console.log("test isWeekend1(): "); | |
console.log(unitTests.testIsWeekend1(isWeekend1)); | |
console.log("test isWeekend2(): "); | |
console.log(unitTests.testIsWeekend2(isWeekend2)); | |
console.log("test isWeekendByDay(): "); | |
console.log(unitTests.testIsWeekendByDay1(isWeekendByDay) === true); | |
console.log(unitTests.testIsWeekendByDay2(isWeekendByDay) === true); | |
console.log("test applyCoupon(): "); | |
console.log(unitTests.testApplyCoupon1(applyCoupon)); | |
</script></body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
console.log("-------------------------------------------------"); | |
// question 1 | |
const allEquals = arr => { | |
return arr.every(item => item === arr[0]); | |
}; | |
const isDual = arr => { | |
// check the length of array | |
// if the length is 0 then return true directly | |
// if the length is a odd number then return false directly | |
if (arr.length === 0) { | |
return 1; | |
} else if (arr.length % 2 === 1) { | |
return 0; | |
} | |
const result = arr.map((item, index) => { | |
// console.log(index); | |
if (index % 2 === 0) { | |
return item + arr[index+1]; | |
} else { | |
return null; | |
} | |
}) | |
.filter(item => item !== null); | |
// check result are equals | |
return allEquals(result) ? 1 : 0; | |
}; | |
// question 2 | |
// solution 1: | |
function isWeekend1() { | |
const todayDate = new Date(); | |
const day = todayDate.getDay(); | |
const isWeekend1 = day => day === 0 || day === 6 | |
const isWeekday1 = day => day >= 1 && day <= 5 | |
let week = [day]; | |
const weekday = week.filter(isWeekday1).map(day => "weekday"); | |
const weekend = week.filter(isWeekend1).map(day => "weekdend"); | |
// console.log(weekday.concat(weekend)[0]); | |
return weekday.concat(weekend)[0]; | |
} | |
// solution 2: | |
function isWeekend2() { | |
const todayDate = new Date(); | |
const day = todayDate.getDay(); | |
return isWeekendByDay(day); | |
} | |
function isWeekendByDay(day) { | |
const map = ["weekend", "weekday", "weekday", "weekday", "weekday", "weekday", "weekend"]; | |
return map[day]; | |
} | |
// question 3 | |
const applyCoupon = category => { | |
return coupon => { | |
return item => { | |
if (item.category === category) { | |
let newPrice = item.price * (1 - coupon); | |
item.price = newPrice; | |
} | |
return item; | |
} | |
} | |
}; | |
// unit test | |
var unitTests = {}; | |
// test isDual() | |
unitTests.testIsDual1 = function(method) { | |
return method([]) === 1; | |
}; | |
unitTests.testIsDual2 = function(method) { | |
return method([1, 2, 3]) === 0; | |
}; | |
unitTests.testIsDual3 = function(method) { | |
return method([1, 2, 3, 0]) === 1; | |
}; | |
unitTests.testIsDual4 = function(method) { | |
return method([1, 1, 2, 2]) === 0; | |
}; | |
// test allEquals() | |
unitTests.testAllEquals1 = function(method) { | |
return method([2, 3]) === false; | |
}; | |
unitTests.testAllEquals2 = function(method) { | |
return method([3, 3]) === true; | |
}; | |
// test isWeekend1() | |
unitTests.testIsWeekend1 = function(method) { | |
return method() == "weekday"; | |
} | |
// test isWeekend2() | |
unitTests.testIsWeekend2 = function(method) { | |
return method() == "weekday"; | |
} | |
// test isWeekendByDay() | |
unitTests.testIsWeekendByDay1 = function(method) { | |
// 0 is Sunday | |
return method(0) === "weekend" && method(6) === "weekend"; | |
} | |
unitTests.testIsWeekendByDay2 = function(method) { | |
return method(1) === "weekday" && method(5) === "weekday"; | |
} | |
// test applyCoupon1() | |
unitTests.testApplyCoupon1 = function(method) { | |
const item = { | |
"name": "Biscuits", | |
"type": "regular", | |
"category": "food", | |
"price": 2.0 | |
} | |
return method("food")(0.1)(item).price === 1.8; | |
} | |
console.log("test isDual(): "); | |
console.log(unitTests.testIsDual1(isDual)); | |
console.log(unitTests.testIsDual2(isDual)); | |
console.log(unitTests.testIsDual3(isDual)); | |
console.log("test allEquals(): "); | |
console.log(unitTests.testAllEquals1(allEquals)); | |
console.log(unitTests.testAllEquals2(allEquals)); | |
console.log("test isWeekend1(): "); | |
console.log(unitTests.testIsWeekend1(isWeekend1)); | |
console.log("test isWeekend2(): "); | |
console.log(unitTests.testIsWeekend2(isWeekend2)); | |
console.log("test isWeekendByDay(): "); | |
console.log(unitTests.testIsWeekendByDay1(isWeekendByDay) === true); | |
console.log(unitTests.testIsWeekendByDay2(isWeekendByDay) === true); | |
console.log("test applyCoupon(): "); | |
console.log(unitTests.testApplyCoupon1(applyCoupon)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment