This question already has answers here:
Javascript if statement with multiple permissible conditions [duplicate]
(4 answers)
Closed 5 years ago.
Why doesn't this work:
if (x != (a || b || c)) {
doStuff();
}
It's meant to check whether x is NOT equal to a OR b OR c.
EDIT: How would I achieve a check for whether x is NOT equal to a OR b OR c?
EDIT: Ok, it's a duplicate. What do I do now, take minus points even after realizing my mistake? :P
To use multiples values like you wanna just:
var x = 'x';
var a = 'a';
var b = 'b';
var c = 'c';
function doStuff() {
console.log(1)
}
// exemple 1
if(x == a || x == b || x == c) {
doStuff();
}
function isSameValue(element, index, array) {
return element === x;
}
// exemple 2
if([a, b, c].some(isSameValue)) {
doStuff();
}
// exemple 3
[a, b, c].includes(x);
Related
This question already has answers here:
Why is 4 not an instance of Number?
(5 answers)
Closed 8 months ago.
I've searched the site for this question and got the following answer:
function instance_of(V, F) {
var O = F.prototype;
V = V.__proto__;
while (true) {
if (V === null)
return false;
if (O === V)
return true;
V = V.__proto__;
}
}
It's from https://stackoverflow.com/a/9220317/12974414.
But I tested the following code, found something weird:
console.log( (10).__proto__ === Number.prototype ) // true
console.log( instance_of(10, Number ) ) // true
console.log( 10 instanceof Number ) // false
function instance_of(V, F) {
var O = F.prototype;
V = V.__proto__;
while (true) {
if (V === null)
return false;
if (O === V)
return true;
V = V.__proto__;
}
}
How to explain this and how does instanceof operator actually work in JS ?
JavaScript has a feature called autoboxing which converts primitives to Objects when you try to access properties on them.
instanceof doesn't autobox and a primitive number is not an instance of the Number class.
You can see, in the specification:
If Type(O) is not Object, return false.
This question already has answers here:
Javascript compare 3 values
(7 answers)
Closed 2 years ago.
I am working on studying for an entry test, and being self learned I have been working a lot of functions problems. this one has stumped me,
I a to write a function testing to see if 3 values are equal. The code i have tired is:
Function equal(a,b,c){
return a==b==c;
}
as well as:
function equal(a,b,c){
let newEqual=a==b;
return newEqual===c
}
I feel like I am missing something rather simple but have not been able to put my finger on it.
thank you in advance for any insight
a == b == c will be evaluated as :
a == b then checks the result ( true ) and compares it with c => true == c which is false :
const a = 5;
const b = 5;
const c = 5;
const result = a == b == c ;
console.log(result); // false
const a1 = 5;
const b1 = 5;
const c1 = true;
const result1 = a1 == b1 == c1 ;
console.log(result1); // true
You should compare them separately :
const a = 5;
const b = 5;
const c = 5;
const result = a == b && b == c ;
console.log(result);
To check whether all three variables are equal or not, use && operator to add on queries.
&& returns true if all conditions are true.
function equal(a,b,c){
return a == b && b == c;
}
As mentioned by #mplungjan, for strict comparison use === instead of ==.
Try this.
function equal(a,b,c){
return a==b && b==c;
}
You can check it by doing the following:
function equal(a,b,c){
return (a==b) && (b==c)
}
That way you are checking if a == b is true, and b == c is true then all three are equal. In other words, true && true = true
Currently, the only way I know of to do what the title states is by writing the following:
var a = 3, b = 5, c = 3;
if (a === b && a === c) {
// code
}
Or by using the ternary operator:
(a === b && a === c) ? /* code */ : /* else */
Is there a way to check a against both b and c? Something like this perhaps:
if (a === (b && c)) {
// code
}
Obviously this doesn't work as intended, which is why I'm asking the question. Any help is appreciated.
This is not a duplicate of the other 2 - those two are using the OR operator. I'm asking about the AND operator.
For a simple case like this? Definitely not. There are some handy little tricks you can use if you build an array, however. For example:
var my_array = [3, 5, 3];
if(my_array.every(function(el) {return el == my_array[0];})) {
// code
}
Unfortunately no. That's what you have. Unless you convert it to an Array and test with a loop.
if a,b and c are all numbers then you can do simple maths to check for parity of b and c against a.
Simply add b and c, divide by 2 to get the average number and use that to compare against a.
var a = 3, b = 5, c = 3;
a === (b + c)/2
? console.log('yes')
: console.log('no')
// console gives "no"
var a = 3, b = 3, c = 3;
a === (b + c)/2
? console.log('yes')
: console.log('no')
// console gives "yes"
var a = 3, b = 4, c = 2;
a === (b + c)/2
? console.log('yes')
: console.log('no')
// console gives "yes"
This question already has answers here:
The logical && and || operators in JavaScript
(2 answers)
Closed 4 years ago.
This code is result why [5,5,5,5] ? "&&" what is doing in code ? Could you light me up
var array1 = [1, 4, 9, 16];
const map1 = array1.map(x => x && 5);
console.log(map1);
In JavaScript, if x is truthy, then x && y will return y.
Your array contains numeric values that are all truthy. If one of your values was falsy, like 0 then x && y would return x instead - give it a try!
Learn more on MDN...
About truthy (links to falsey)
About the logical shortcut operators
x => x && 5 basically behaves the same as:
x => {
if (x) {
return 5;
} else {
return x;
}
}
This question already has answers here:
The 3 different equals
(5 answers)
Closed 6 years ago.
I am working on code for a site am trying to take an input and based on the input display a specific message. Whenever I type in a number however it is being set to the first if result. Any help would be great, thanks
<form name="testsDone">
How many of your tests have you taken? <input type ="text" id="tookTests" /><br />
</form>
<script>
function testCheck(){
var a = parseFloat(document.getElementById("tookTests").value);
var b = a;
var c;
if (b = 2 ) {
c = "Yes you can! You've earned it.";
} else if (b = 1) {
c = "You need to keep studying then";
}
else if (b > 2 ) {
c = "That's more tests than you're supposed to have";
}else {
c = "Why are you on here? You need to study.";
}
document.getElementById("change").innerHTML = c;
}
</script>
<button onclick="testCheck()">Check and See</button>
<p> Can you go to sleep?</p>
<p id="change"></p>
You are assigning value here..
if (b = 2 ) {
it should be..
if (b == 2 ) {
Hey change it to the following
function testCheck(){
var a = parseFloat(document.getElementById("tookTests").value);
var b = a;
var c;
if (b == 2 ) {
c = "Yes you can! You've earned it.";
} else if (b == 1) {
c = "You need to keep studying then";
}
else if (b > 2 ) {
c = "That's more tests than you're supposed to have";
}else {
c = "Why are you on here? You need to study.";
}
document.getElementById("change").innerHTML = c;
}