Skip to content

Instantly share code, notes, and snippets.

@richfergus
Created February 22, 2018 23:21
Show Gist options
  • Save richfergus/6c61ba66b5b1833b3e9448e47db5b929 to your computer and use it in GitHub Desktop.
Save richfergus/6c61ba66b5b1833b3e9448e47db5b929 to your computer and use it in GitHub Desktop.
JS Bin ES6 Exercise: rest/spread // source http://jsbin.com/racoqor
<!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';
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 ( ...values ) {};
/**
* 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 ) {};
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 ) {};
/**
* 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 ) {};
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';
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 ( ...values ) {};
/**
* 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 ) {};
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