Last active
June 19, 2020 05:08
-
-
Save xavierchia/aa56fadaea9c0a68ceb4684d0276daef to your computer and use it in GitHub Desktop.
This file contains hidden or 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
<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