Files
.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
formattingConversion
math-challenges
requiresTests
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
guide
mock-guide
search-indexing
tools
.editorconfig
.eslintignore
.eslintrc.json
.gitattributes
.gitignore
.node-inspectorrc
.prettierrc
.snyk
.travis.yml
.vcmrc
CODE_OF_CONDUCT.md
CONTRIBUTING.md
Dockerfile.tests
LICENSE.md
README.french.md
README.italian.md
README.korean.md
README.md
SECURITY.md
azure-pipelines.yml
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
freeCodeCamp/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/debugging/catch-use-of-assignment-operator-instead-of-equality-operator.english.md

87 lines
2.6 KiB
Markdown
Raw Normal View History

---
id: 587d7b85367417b2b2512b38
title: Catch Use of Assignment Operator Instead of Equality Operator
challengeType: 1
---
## Description
<section id='description'>
Branching programs, i.e. ones that do different things if certain conditions are met, rely on <code>if</code>, <code>else if</code>, and <code>else</code> statements in JavaScript. The condition sometimes takes the form of testing whether a result is equal to a value.
This logic is spoken (in English, at least) as "if x equals y, then ..." which can literally translate into code using the <code>=</code>, or assignment operator. This leads to unexpected control flow in your program.
As covered in previous challenges, the assignment operator (<code>=</code>) in JavaScript assigns a value to a variable name. And the <code>==</code> and <code>===</code> operators check for equality (the triple <code>===</code> tests for strict equality, meaning both value and type are the same).
The code below assigns <code>x</code> to be 2, which evaluates as <code>true</code>. Almost every value on its own in JavaScript evaluates to <code>true</code>, except what are known as the "falsy" values: <code>false</code>, <code>0</code>, <code>""</code> (an empty string), <code>NaN</code>, <code>undefined</code>, and <code>null</code>.
```js
let x = 1;
let y = 2;
if (x = y) {
// this code block will run for any value of y (unless y were originally set as a falsy)
} else {
// this code block is what should run (but won't) in this example
}
```
</section>
## Instructions
<section id='instructions'>
Fix the condition so the program runs the right branch, and the appropriate value is assigned to <code>result</code>.
</section>
## Tests
<section id='tests'>
```yml
tests:
- text: Your code should fix the condition so it checks for equality, instead of using assignment.
testString: assert(result == "Not equal!", 'Your code should fix the condition so it checks for equality, instead of using assignment.');
- text: The condition can use either <code>==</code> or <code>===</code> to test for equality.
testString: assert(code.match(/x\s*?===?\s*?y/g), 'The condition can use either <code>==</code> or <code>===</code> to test for equality.');
```
</section>
## Challenge Seed
<section id='challengeSeed'>
<div id='js-seed'>
```js
let x = 7;
let y = 9;
let result = "to come";
if(x = y) {
result = "Equal!";
} else {
result = "Not equal!";
}
console.log(result);
```
</div>
</section>
## Solution
<section id='solution'>
```js
let x = 7;
let y = 9;
let result = "to come";
if(x === y) {
result = "Equal!";
} else {
result = "Not equal!";
}
console.log(result);
```
</section>