44 lines
1.5 KiB
Markdown
44 lines
1.5 KiB
Markdown
|
---
|
||
|
title: Comparison with the Equality Operator
|
||
|
---
|
||
|
## Comparison with the equality operator
|
||
|
|
||
|
|
||
|
### Problem explanation:
|
||
|
_Add the equality operator to the indicated line so that the function will return "Equal" when `val` is equivalent to 12._
|
||
|
|
||
|
#### Hint 1
|
||
|
Remember that _equality is different from assignment (`=`), which assigns the value at the right of the operator to a variable in the left._<sup><a href="#cite1">1</a></sup>
|
||
|
> _try to solve the problem now_
|
||
|
|
||
|
|
||
|
## Spoiler alert!
|
||
|
|
||
|
**Solution ahead!**
|
||
|
|
||
|
## Basic code solution:
|
||
|
|
||
|
```javascript
|
||
|
|
||
|
function testEqual(val) {
|
||
|
if (val == 12) { // Change this line
|
||
|
return "Equal";
|
||
|
}
|
||
|
return "Not equal";
|
||
|
}
|
||
|
// Change this value to test
|
||
|
testEqual(10);
|
||
|
|
||
|
```
|
||
|
· [Run code at repl.it](https://repl.it/@AdrianSkar/Basic-JS-Equality-operator)
|
||
|
|
||
|
### Code explanation
|
||
|
The function first evaluates `if` the condition `(val == 12)` evaluates to `true`. If it does, it returns the statement between the curly braces ("Equal"). If it doesn't, it returns the next `return` statement outside them ("Not equal").
|
||
|
|
||
|
### Sources
|
||
|
<span id="cite1">1</span>. ["Basic JavaScript: Comparison with the Equality Operator", fCC lesson at *Javascript Algorithms And Data Structures Certification*](https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/comparison-with-the-equality-operator)
|
||
|
|
||
|
### Resources
|
||
|
|
||
|
- ["Equality operator" - *MDN JavaScript reference*](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators#Equality_())
|