From e18c6955b06a41b41c90ece27dbc1e9c3d185006 Mon Sep 17 00:00:00 2001 From: Randell Dawson <5313213+RandellDawson@users.noreply.github.com> Date: Sat, 20 Mar 2021 06:23:32 -0600 Subject: [PATCH] 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 --- ...rite-reusable-javascript-with-functions.md | 64 ++++++++----------- 1 file changed, 28 insertions(+), 36 deletions(-) diff --git a/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/basic-javascript/write-reusable-javascript-with-functions.md b/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/basic-javascript/write-reusable-javascript-with-functions.md index fbf6431d58..2a2e73da5e 100644 --- a/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/basic-javascript/write-reusable-javascript-with-functions.md +++ b/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/basic-javascript/write-reusable-javascript-with-functions.md @@ -33,57 +33,49 @@ You can call or invoke 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--