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:
Randell Dawson
2021-03-20 06:23:32 -06:00
committed by GitHub
parent 4cd40f23d1
commit e18c6955b0

View File

@ -33,57 +33,49 @@ 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--
```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--
```js
uncapture();
if (typeof reusableFunction !== "function") {
(function() { return "reusableFunction is not defined"; })();
} else {
(function() { return logOutput || "console.log never called"; })();
function testConsole() {
var logOutput = "";
var originalConsole = console;
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--