* Fix tests - fix original log() method restoration - fix case that other userscripts(or extensions) can affect console output and therefore can ruin the test * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/basic-javascript/write-reusable-javascript-with-functions.english.md allow function calling without semicolon Co-Authored-By: T1mL3arn <T1mL3arn@users.noreply.github.com>
2.8 KiB
2.8 KiB
id, title, challengeType, videoUrl
id | title | challengeType | videoUrl |
---|---|---|---|
56bbb991ad1ed5201cd392cf | Write Reusable JavaScript with Functions | 1 | https://scrimba.com/c/cL6dqfy |
Description
function functionName() {You can call or invoke this function by using its name followed by parentheses, like this:
console.log("Hello World");
}
functionName();
Each time the function is called it will print out the message "Hello World"
on the dev console. All of the code between the curly braces will be executed every time the function is called.
Instructions
- Create a function called
reusableFunction
which prints"Hi World"
to the dev console. - Call the function.
Tests
tests:
- text: <code>reusableFunction</code> should be a function
testString: assert(typeof reusableFunction === 'function', '<code>reusableFunction</code> should be a function');
- text: <code>reusableFunction</code> should output "Hi World" to the dev console
testString: assert(hiWorldWasLogged, '<code>reusableFunction</code> should output "Hi World" to the dev console');
- text: Call <code>reusableFunction</code> after you define it
testString: assert(/^\s*reusableFunction\(\)\s*/m.test(code), 'Call <code>reusableFunction</code> after you define it');
Challenge Seed
// Example
function ourReusableFunction() {
console.log("Heyya, World");
}
ourReusableFunction();
// Only change code below this line
Before Test
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 Test
uncapture();
if (typeof reusableFunction !== "function") {
(function() { return "reusableFunction is not defined"; })();
} else {
(function() { return logOutput || "console.log never called"; })();
}
Solution
function reusableFunction() {
console.log("Hi World");
}
reusableFunction();