fix(curriculum): Improve test to allow any valid function call for Write Reusable JavaScript with Functions challenge (#41538)
* fix: improve test to allow function call anywhere * fix: force user to put console.log in function
This commit is contained in:
@ -33,30 +33,36 @@ You can call or <dfn>invoke</dfn> this function by using its name followed by pa
|
||||
assert(typeof reusableFunction === 'function');
|
||||
```
|
||||
|
||||
`reusableFunction` should output the string `Hi World` to the console.
|
||||
If `reusableFunction` is called, it should output the string `Hi World` to the console.
|
||||
|
||||
```js
|
||||
assert(hiWorldWasLogged);
|
||||
assert(testConsole());
|
||||
```
|
||||
|
||||
You should call `reusableFunction` after you define it.
|
||||
You should call `reusableFunction` once it is defined.
|
||||
|
||||
```js
|
||||
assert(/^\s*reusableFunction\(\)\s*/m.test(code));
|
||||
const functionStr = reusableFunction && __helpers.removeWhiteSpace(reusableFunction.toString());
|
||||
const codeWithoutFunction = __helpers.removeWhiteSpace(code).replace(/reusableFunction\(\)\{/g, '');
|
||||
assert(/reusableFunction\(\)/.test(codeWithoutFunction));
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --before-user-code--
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
var logOutput = "";
|
||||
var originalConsole = console;
|
||||
var nativeLog = console.log;
|
||||
var hiWorldWasLogged = false;
|
||||
function capture() {
|
||||
|
||||
function testConsole() {
|
||||
var logOutput = "";
|
||||
var originalConsole = console;
|
||||
var nativeLog = console.log;
|
||||
var hiWorldWasLogged = false;
|
||||
console.log = function (message) {
|
||||
if(message === 'Hi World') hiWorldWasLogged = true;
|
||||
if(message === 'Hi World') {
|
||||
console.warn(message)
|
||||
hiWorldWasLogged = true;
|
||||
}
|
||||
if(message && message.trim) logOutput = message.trim();
|
||||
if(nativeLog.apply) {
|
||||
nativeLog.apply(originalConsole, arguments);
|
||||
@ -65,25 +71,11 @@ function capture() {
|
||||
nativeLog(nativeMsg);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
function uncapture() {
|
||||
reusableFunction();
|
||||
console.log = nativeLog;
|
||||
return hiWorldWasLogged;
|
||||
}
|
||||
|
||||
capture();
|
||||
```
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
uncapture();
|
||||
|
||||
if (typeof reusableFunction !== "function") {
|
||||
(function() { return "reusableFunction is not defined"; })();
|
||||
} else {
|
||||
(function() { return logOutput || "console.log never called"; })();
|
||||
}
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
Reference in New Issue
Block a user