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,57 +33,49 @@ You can call or <dfn>invoke</dfn> this function by using its name followed by pa
|
|||||||
assert(typeof reusableFunction === 'function');
|
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
|
```js
|
||||||
assert(hiWorldWasLogged);
|
assert(testConsole());
|
||||||
```
|
```
|
||||||
|
|
||||||
You should call `reusableFunction` after you define it.
|
You should call `reusableFunction` once it is defined.
|
||||||
|
|
||||||
```js
|
```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--
|
# --seed--
|
||||||
|
|
||||||
## --before-user-code--
|
|
||||||
|
|
||||||
```js
|
|
||||||
var logOutput = "";
|
|
||||||
var originalConsole = console;
|
|
||||||
var nativeLog = console.log;
|
|
||||||
var hiWorldWasLogged = false;
|
|
||||||
function capture() {
|
|
||||||
console.log = function (message) {
|
|
||||||
if(message === 'Hi World') hiWorldWasLogged = true;
|
|
||||||
if(message && message.trim) logOutput = message.trim();
|
|
||||||
if(nativeLog.apply) {
|
|
||||||
nativeLog.apply(originalConsole, arguments);
|
|
||||||
} else {
|
|
||||||
var nativeMsg = Array.prototype.slice.apply(arguments).join(' ');
|
|
||||||
nativeLog(nativeMsg);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
function uncapture() {
|
|
||||||
console.log = nativeLog;
|
|
||||||
}
|
|
||||||
|
|
||||||
capture();
|
|
||||||
```
|
|
||||||
|
|
||||||
## --after-user-code--
|
## --after-user-code--
|
||||||
|
|
||||||
```js
|
```js
|
||||||
uncapture();
|
|
||||||
|
|
||||||
if (typeof reusableFunction !== "function") {
|
function testConsole() {
|
||||||
(function() { return "reusableFunction is not defined"; })();
|
var logOutput = "";
|
||||||
} else {
|
var originalConsole = console;
|
||||||
(function() { return logOutput || "console.log never called"; })();
|
var nativeLog = console.log;
|
||||||
|
var hiWorldWasLogged = false;
|
||||||
|
console.log = function (message) {
|
||||||
|
if(message === 'Hi World') {
|
||||||
|
console.warn(message)
|
||||||
|
hiWorldWasLogged = true;
|
||||||
|
}
|
||||||
|
if(message && message.trim) logOutput = message.trim();
|
||||||
|
if(nativeLog.apply) {
|
||||||
|
nativeLog.apply(originalConsole, arguments);
|
||||||
|
} else {
|
||||||
|
var nativeMsg = Array.prototype.slice.apply(arguments).join(' ');
|
||||||
|
nativeLog(nativeMsg);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
reusableFunction();
|
||||||
|
console.log = nativeLog;
|
||||||
|
return hiWorldWasLogged;
|
||||||
}
|
}
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## --seed-contents--
|
## --seed-contents--
|
||||||
|
Reference in New Issue
Block a user