.github
api-server
client
config
curriculum
challenges
_meta
arabic
chinese
english
01-responsive-web-design
02-javascript-algorithms-and-data-structures
basic-algorithm-scripting
basic-data-structures
basic-javascript
debugging
catch-arguments-passed-in-the-wrong-order-when-calling-a-function.english.md
catch-missing-open-and-closing-parenthesis-after-a-function-call.english.md
catch-misspelled-variable-and-function-names.english.md
catch-mixed-usage-of-single-and-double-quotes.english.md
catch-off-by-one-errors-when-using-indexing.english.md
catch-unclosed-parentheses-brackets-braces-and-quotes.english.md
catch-use-of-assignment-operator-instead-of-equality-operator.english.md
prevent-infinite-loops-with-a-valid-terminal-condition.english.md
understanding-the-differences-between-the-freecodecamp-and-browser-console.english.md
use-caution-when-reinitializing-variables-inside-a-loop.english.md
use-the-javascript-console-to-check-the-value-of-a-variable.english.md
use-typeof-to-check-the-type-of-a-variable.english.md
es6
functional-programming
intermediate-algorithm-scripting
javascript-algorithms-and-data-structures-projects
object-oriented-programming
regular-expressions
03-front-end-libraries
04-data-visualization
05-apis-and-microservices
06-information-security-and-quality-assurance
08-coding-interview-prep
09-certificates
portuguese
russian
spanish
schema
test
.babelrc
.editorconfig
.npmignore
.travis.yml
CHANGELOG.md
LICENSE.md
commitizen.config.js
commitlint.config.js
create-challenge-bundle.js
getChallenges.js
gulpfile.js
index.js
lib.js
md-translation.js
package-entry.js
package-lock.json
package.json
utils.js
docs
search-indexing
tools
utils
.editorconfig
.eslintignore
.eslintrc.json
.gitattributes
.gitignore
.gitpod.yml
.node-inspectorrc
.prettierignore
.prettierrc
.snyk
.travis.yml
.vcmrc
CODE_OF_CONDUCT.md
CONTRIBUTING.md
Dockerfile.tests
LICENSE.md
README.md
SECURITY.md
change_volumes_owner.sh
docker-compose-shared.yml
docker-compose.tests.yml
docker-compose.yml
lerna.json
libcimp_index_js.patch
package-lock.json
package.json
patch_npm_and_install.sh
sample.env
77 lines
3.1 KiB
Markdown
77 lines
3.1 KiB
Markdown
---
|
|
id: 587d7b83367417b2b2512b37
|
|
title: Understanding the Differences between the freeCodeCamp and Browser Console
|
|
challengeType: 1
|
|
forumTopicId: 301193
|
|
---
|
|
|
|
## Description
|
|
<section id='description'>
|
|
You may have noticed that some freeCodeCamp JavaScript challenges include their own console. This console behaves a little differently than the browser console you used in the last challenge.
|
|
The following challenge is meant to highlight the main difference between the freeCodeCamp console and your browser console.
|
|
When you run ordinary JavaScript, the browsers console will display your <code>console.log()</code> statements the exact number of times you requested.
|
|
The freeCodeCamp console will print your <code>console.log()</code> statements for each test of that challenge, as well as one more time for any function calls that you have in your code.
|
|
This lends itself to some interesting behavior and might trip you up in the beginning, because a logged value that you expect to see only once may print out many more times.
|
|
If you would like to see only your single output and not have to worry about running through the test cycles, you can use <code>console.clear()</code> and check the browsers console.
|
|
</section>
|
|
|
|
## Instructions
|
|
<section id='instructions'>
|
|
First, use <code>console.clear()</code> to clear the browser console. After that, use <code>console.log</code> to log the <code>output</code> variable.
|
|
</section>
|
|
|
|
## Tests
|
|
<section id='tests'>
|
|
|
|
```yml
|
|
tests:
|
|
- text: You should use <code>console.clear()</code> to clear the browser console.
|
|
testString: const removeJSComments = code.replace(/\/\*[\s\S]*?\*\/|\/\/.*$/gm, ''); const noSpaces = removeJSComments.replace(/\s/g, ''); assert(noSpaces.match(/console.clear\(\)/));
|
|
- text: You should use <code>console.log()</code> to print the <code>output</code> variable.
|
|
testString: const noSpaces = code.replace(/\s/g, ''); assert(noSpaces.match(/console\.log\(output\)/));
|
|
|
|
```
|
|
|
|
</section>
|
|
|
|
## Challenge Seed
|
|
<section id='challengeSeed'>
|
|
|
|
<div id='js-seed'>
|
|
|
|
```js
|
|
// Open your browser console.
|
|
let output = "Get this to log once in the browser console and twice in the freeCodeCamp console";
|
|
// Use console.clear() on the next line to clear the browser console.
|
|
|
|
|
|
// Use console.log() to print the output variable.
|
|
|
|
|
|
// Check the two consoles to see the difference. The freeCodeCamp console should have printed the variable twice, once for each test of this challenge. The browser console should only print the variable once because you cleared it first.
|
|
```
|
|
|
|
</div>
|
|
|
|
|
|
|
|
</section>
|
|
|
|
## Solution
|
|
<section id='solution'>
|
|
|
|
|
|
```js
|
|
// Open your browser console.
|
|
let output = "Get this to log once in the browser console and twice in the freeCodeCamp console";
|
|
// Use console.clear() on the next line to clear the browser console.
|
|
console.clear();
|
|
|
|
// Use console.log() to print the output variable.
|
|
console.log(output);
|
|
|
|
// Check the two consoles to see the difference. The freeCodeCamp console should have printed the variable twice, one for each test of this challenge. The browser console should only print the variable once becuase you cleared it first.
|
|
```
|
|
|
|
</section>
|