Skip to content

Instantly share code, notes, and snippets.

@xavierchia
Last active June 19, 2020 05:08
Show Gist options
  • Save xavierchia/aa56fadaea9c0a68ceb4684d0276daef to your computer and use it in GitHub Desktop.
Save xavierchia/aa56fadaea9c0a68ceb4684d0276daef to your computer and use it in GitHub Desktop.
<script src="simpletest.js"></script>
<script>
/*
The runWithDebugger function should run a callback with a debugger
Syntax:
Formula: runWithDebugger(callback, [callbackArguments])
Arguments:
callback
[callbackArguments]
Return Value: undefined
Tests:
callback:
It should take a callback function as the first argument [DONE]
It should run the callback function [DONE]
callbackArguments:
It should take an array as the optional second argument [DONE]
It should input the array elements as the callback arguments
*/
function runWithDebugger(callback, callbackArguments) {
debugger;
if (toString.call(callback) !== '[object Function]') {
throw new TypeError('I am not a function');
}
if (callbackArguments) {
if (!Array.isArray(callbackArguments)) {
throw new TypeError('I am not an array')
}
return callback(...callbackArguments);
}
return callback();
}
tests({
'It should take a callback function as the first argument': function () {
var isTypeError = false;
try {
runWithDebugger('I am not a function');
}
catch (e) {
isTypeError = e instanceof TypeError;
}
eq(isTypeError, true);
},
'It should run the callback function': function() {
var callbackRan = 0;
runWithDebugger(function() {
callbackRan++;
})
eq(callbackRan, 1);
},
'It should take an array as the optional second argument': function() {
var isTypeError = false;
try {
runWithDebugger(function(){},'I am not an array');
}
catch (e) {
isTypeError = e instanceof TypeError;
}
eq(isTypeError, true);
},
'It should input the array elements as the callback arguments': function() {
debugger;
var result = runWithDebugger(function addNumbers(numOne, numTwo, numThree) {
return numOne + numTwo + numThree
}, [1,2,3])
eq(result, 6);
}
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment