Skip to content

Instantly share code, notes, and snippets.

@millerdrew
Created March 1, 2017 18:01
Show Gist options
  • Save millerdrew/d4072a412cc3160a6c67ec45c9fa66e4 to your computer and use it in GitHub Desktop.
Save millerdrew/d4072a412cc3160a6c67ec45c9fa66e4 to your computer and use it in GitHub Desktop.
JS Bin ES6 Exercise: rest/spread // source https://jsbin.com/lalalahevi
<!DOCTYPE html>
<html>
<head>
<meta name="description" content="ES6 Exercise: rest/spread">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<h1>Test Results</h1>
<script src="https://wzrd.in/standalone/tap-browser-color@latest"></script>
<script src="https://wzrd.in/standalone/tape@latest"></script>
<script id="jsbin-javascript">
// This will colour the body red/green/yellow to indicate test status.
'use strict';
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) arr2[i] = arr[i]; return arr2; } else { return Array.from(arr); } }
tapBrowserColor();
/**
* ES5 Version of rest parameters
* (the old syntax)
*/
var sum = undefined;
sum = function () {
var total = 0;
for (var i = 0; i < arguments.length; i++) {
total = total + arguments[i];
}
return total;
};
/**
* ES6 Version of Rest Parameters
*/
// TODO: Uncomment the below and flesh it out using ES6 syntax
// and see if the test still passes.
sum = function () {
var total = 0;
for (var _len = arguments.length, values = Array(_len), _key = 0; _key < _len; _key++) {
values[_key] = arguments[_key];
}
values.forEach(function (element) {
total += element;
});
return total;
};
/**
* Tests of Rest Parameters
*/
tape('rest parameters', function (t) {
var res = sum(1, 2, 3);
t.equal(res, 6, 'should total all numbers provided');
t.end();
});
/**
* ES5 Version of spread
* (the old syntax)
*/
var sumArray = undefined;
sumArray = function (array) {
return total = sum.apply(this, array);
};
/**
* ES6 Version of Rest Parameters
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/rest_parameters
*/
// TODO: Uncomment the below and flesh it out using ES6 syntax
// and see if the test still passes.
sumArray = function (values) {
return total = sum.apply(undefined, _toConsumableArray(values));
};
tape('spread', function (t) {
var res = sumArray([1, 2, 3]);
t.equal(res, 6, 'should total all numbers provided');
t.end();
});
</script>
<script id="jsbin-source-javascript" type="text/javascript">// This will colour the body red/green/yellow to indicate test status.
tapBrowserColor();
/**
* ES5 Version of rest parameters
* (the old syntax)
*/
let sum;
sum = function () {
let total = 0;
for ( let i = 0; i < arguments.length; i++ ) {
total = total + arguments[i];
}
return total;
};
/**
* ES6 Version of Rest Parameters
*/
// TODO: Uncomment the below and flesh it out using ES6 syntax
// and see if the test still passes.
sum = function ( ...values ) {
let total = 0;
values.forEach( function(element) {
total += element;
});
return total;
};
/**
* Tests of Rest Parameters
*/
tape( 'rest parameters', t => {
const res = sum( 1, 2, 3 );
t.equal( res, 6, 'should total all numbers provided' );
t.end();
});
/**
* ES5 Version of spread
* (the old syntax)
*/
let sumArray;
sumArray = function ( array ) {
return total = sum.apply( this, array );
};
/**
* ES6 Version of Rest Parameters
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/rest_parameters
*/
// TODO: Uncomment the below and flesh it out using ES6 syntax
// and see if the test still passes.
sumArray = function ( values ) {
return total = sum(...values)
};
tape( 'spread', t => {
const res = sumArray([ 1, 2, 3 ]);
t.equal( res, 6, 'should total all numbers provided' );
t.end();
});
</script></body>
</html>
// This will colour the body red/green/yellow to indicate test status.
'use strict';
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) arr2[i] = arr[i]; return arr2; } else { return Array.from(arr); } }
tapBrowserColor();
/**
* ES5 Version of rest parameters
* (the old syntax)
*/
var sum = undefined;
sum = function () {
var total = 0;
for (var i = 0; i < arguments.length; i++) {
total = total + arguments[i];
}
return total;
};
/**
* ES6 Version of Rest Parameters
*/
// TODO: Uncomment the below and flesh it out using ES6 syntax
// and see if the test still passes.
sum = function () {
var total = 0;
for (var _len = arguments.length, values = Array(_len), _key = 0; _key < _len; _key++) {
values[_key] = arguments[_key];
}
values.forEach(function (element) {
total += element;
});
return total;
};
/**
* Tests of Rest Parameters
*/
tape('rest parameters', function (t) {
var res = sum(1, 2, 3);
t.equal(res, 6, 'should total all numbers provided');
t.end();
});
/**
* ES5 Version of spread
* (the old syntax)
*/
var sumArray = undefined;
sumArray = function (array) {
return total = sum.apply(this, array);
};
/**
* ES6 Version of Rest Parameters
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/rest_parameters
*/
// TODO: Uncomment the below and flesh it out using ES6 syntax
// and see if the test still passes.
sumArray = function (values) {
return total = sum.apply(undefined, _toConsumableArray(values));
};
tape('spread', function (t) {
var res = sumArray([1, 2, 3]);
t.equal(res, 6, 'should total all numbers provided');
t.end();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment