Skip to content

Instantly share code, notes, and snippets.

@daxeh
Created October 22, 2018 09:13
Show Gist options
  • Save daxeh/24df2eea25b8f126b2a44c8d4818dfa9 to your computer and use it in GitHub Desktop.
Save daxeh/24df2eea25b8f126b2a44c8d4818dfa9 to your computer and use it in GitHub Desktop.
JS Bin mirus-problem1 // source https://jsbin.com/wuxajij
<!DOCTYPE html>
<html>
<head>
<meta name="description" content="mirus-problem1">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<script id="jsbin-javascript">
/**
* @param {...args} any integer
* @return {number} sum of integers
*/
"use strict";
function sum() {
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
// Parse arg as number by bitwise operator, defaults 0
var arr = Array.from(arguments, function (x) {
return ~ ~x | 0;
});
return arr.reduce(function (acc, num) {
return acc += num;
}, 0);
}
// sum(0);
// sum(1);
// sum(1,2,3);
// sum({a:1}, 1);
// sum(null,null);
// sum("-1",-1,-1);
</script>
<script id="jsbin-source-javascript" type="text/javascript">
/**
* @param {...args} any integer
* @return {number} sum of integers
*/
function sum(...args) {
// Parse arg as number by bitwise operator, defaults 0
const arr =
Array.from(arguments, x => ~~x | 0);
return arr.reduce((acc, num) => acc += num, 0);
}
// sum(0);
// sum(1);
// sum(1,2,3);
// sum({a:1}, 1);
// sum(null,null);
// sum("-1",-1,-1);
</script></body>
<script src="https://unpkg.com/jest-expect-standalone@latest/dist/expect.min.js"></script>
<script>
const test = [
{f: [0], exp: 0},
{f: [1], exp: 1},
{f: [1,2,3], exp: 6},
{f: [{a:1}, 1], exp: 1},
{f: [null, null], exp: 0}
];
test.map(x => {
const {f, exp} = x;
const actual = sum.apply(null, f);
try {
console.log(`Testcase: ${f}`);
window.expect(actual).toBe(exp);
console.log(` -> fn(${f}) has passed`);
} catch(e) {
console.error(`**fn(${f}) => [${actual}]`);
}
});
</script>
</html>
/**
* @param {...args} any integer
* @return {number} sum of integers
*/
"use strict";
function sum() {
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
// Parse arg as number by bitwise operator, defaults 0
var arr = Array.from(arguments, function (x) {
return ~ ~x | 0;
});
return arr.reduce(function (acc, num) {
return acc += num;
}, 0);
}
// sum(0);
// sum(1);
// sum(1,2,3);
// sum({a:1}, 1);
// sum(null,null);
// sum("-1",-1,-1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment