How to check for two conditions using && operator [closed] - javascript

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I'm trying to use 1 If statement to check that both values are not zero
if ((minvalue !== 0) && (maxvalue !== 0)) {
// Both are not 0
}
else
{
// Both values are 0
}
I can get it to work by using two if statements
if ((minvalue !== 0){
if(maxvalue !== 0){
// Both values are not zero
}
}
But I'm not sure how to do it in one If.

This will also work
if (minvalue || maxvalue) {
// Both are not 0
}else {
// Both values are 0
}
Edit :
If you example doesn't work, you should consider doing
console.log(minvalue,maxvalue);
Your code works, so that's your minvalue and maxvalue which are wrong. Might be strings

Your first code block should be fine. If you want to get a bit more clever about it you could instead test that the product of both values is not zero (as anything multiplied by zero will be zero).
const minvalue = 1
const maxvalue = 3
if (minvalue * maxvalue !== 0) {
console.log('foo!') // foo!
} else {
console.log('bar...') // [not hit]
}
Also, stylistically, it's considered bad practice to leave hanging curly braces. Move your else and subsequent opening block curly brace up a line (as above).

Related

What Is The Difference Between else and !== In JavaScript? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 months ago.
Improve this question
Is there any difference between else and !== in JavaScript?
I am beginner in JavaScript and I can't find their difference, for example in this code I expected the text to appear when I enter other days.
const day= prompt(`Insert The Day`);
if(day===`monday`){
console.log(`Pray All Day Long🙏🏽`)
}
else if(day===`tuesday`){
console.log(`Code All Day Long`)
}
else if(day===`wednesday`){
console.log(`Meditate All Day Long`)
}
else if(day===`Thursday`){
console.log(`Design All Day Long`)
}
else if(day!==`monday`){
console.log(`Why Not Monday?`)
}
else{
console.log(`Invalid Day`)
}
These 2 are entirely different operators.
else is to be applied after an if block, and is run if the condition from the if block wasn't met.
== is a "loose" equals, which means that values of different types with the same value are considered to be equal. This means that 1 == "1" will return true, even if 1 is an int and "1" is a string.
=== checks for absolute equality, which means that only values with the same type and value are considered equal. This means that 1 === "1" is false, but 1 === 1 is true.
!== is the opposite of ===, which means that any value with a different type or value is considered not equal. This means that 1 !== 2 is true, and 1 !== "1" is as well.
Firstly, I think you should learn about if..else. You can read more here, and the !== is an operator
And in your case, I suggest using switch
switch(day) {
case 'monday': console.log('Pray All Day Long🙏🏽');
break;
case 'tuesday': console.log('Code All Day Long');
break;
...
default: console.log('Invalid Day');
break;
}
Finally, the better performance sulotions are Map and using [key, value] of Object
Hope useful for you.

Multiple conditions with logical AND operator in JavaScript [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I'm new to JavaScript and trying to implement a logical AND statement such that the field has a character limit greater than 0 and less than or equal to 100. Where's my try:
document.getElementById('order-content').onkeyup = () => {
if (document.getElementById('order-content').value.length > 0 &&
document.getElementById('order-content').value.length <= 100 ) {
document.getElementById('order-button').disabled = false;
} else {
document.getElementById('order-button').disabled = true;
}
This is not working and the syntax is undoubtedly incorrect. Can anyone help me with the way to implement this properly?
Thanks!
You can try something like this:
The requirements of the length are length > 0 and length <= 100. The expression (length > 0 && length <= 100 ) will evaluate to true if the length passes the requirements. The exclamation point reverses the boolean.
document.getElementById('order-content').onkeyup = () => {
let length = document.getElementById('order-content').value.length;
if (!(length > 0 && length <= 100 )){
document.getElementById('order-button').disabled = false;
} else {
document.getElementById('order-button').disabled = true;
}

Using == in javascript always false [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
Hello I use a javascript function to change my HTML background color by DI state
here's my code
function pageData() {
var DI1_STATE =document.getElementById('DI1').textContent; //load DI1
console.log(DI1_STATE); //DI1_STATE= ON or OFF(TYPEOF = String)
console.log(DI1_STATE=='ON'); //ALWAYS FLASE
console.log(DI1_STATE=='OFF'); //ALWAYS FLASE
var result = DI1_STATE.localeCompare('ON'); //WORK preset 1(TRUE) or -1(FLASE)
console.log(result);
if (DI1_STATE == 'ON'){
document.getElementById('DI1').style.backgroundColor = 'Coral';
document.getElementById('DI1').style.color = 'White';}
else{
document.getElementById('DI1').style.backgroundColor = '#ccc';
document.getElementById('DI1').style.color = 'black';}}
I wonder why == is not work
the whole Html code
I made the server at a microchip,i update the "DI1" by getsensorDATA3()
and the server command below
You always have to check for line breaks, spaces or other non visible characters when comparing string values from html elements. Try
var DI1_STATE =document.getElementById('DI1').textContent.trim()
localeCompare is used to determine sort order and only reports back if the reference string comes before or after the comparison string (in the sort order):
Negative when the referenceStr occurs before compareString
Positive when the referenceStr occurs after compareString
Returns 0 if they are equivalent
You might use it like array.sort((a, b) => b.localeCompare(a));
Tests....
let test = document.getElementsByTagName('textarea')[0].value;
console.log('"' + test + '"')
console.log('test == "TEST"', test == "TEST") // false
console.log('test.localeCompare("TEST")', test.localeCompare("TEST")) // 1
test = test.trim();
console.log('test == "TEST"', test == "TEST") // true
console.log('test.localeCompare("TEST")', test.localeCompare("TEST")) // 0
<textarea>TEST </textarea>

simplify function to a single return statement [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I'm a bit confused as I was asked to simplify this function down to a single return statement, and I'm not exactly sure how I would do that as I'm not sure what the paradigm is for angular / typescript with this.
get remainingSpend() {
if (this.spend >= 0) {
if (this.spend- this.organization.total <= 0) {return '';}
return this.spend - this.organization.total;
} else {
return '';
}
}
You can simplify it with a ternary operator like so:
get remainingSpend(){
return this.spend < 0 || this.spend <= this.organization.total ? ''
: this.spend - this.organization.total
}
get remainingSpend() {
return (this.spend >= 0 && this.spend > this.organization.total) ?
this.spend - this.organization.total : ''
}

numbers and words not numbers to words js [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a simple code gotten from the internet and it did not answer what I really wanted as output. I have two input fields; one for the input and another for the output and they are processed through this function:
<script type="text/javascript">
function AnEventHasOccurred() {
var x = document.getElementById("onkeyup").value
if (x >= "100") {
document.getElementById("eventlog").value = "" +
return x = ['Generalities'];
}
}
</script>
What I'm really needing is that when I enter numbers below 100, output must be Generalities. I haven't got it correctly. And I went here to ask some help. Thanks.
You're never outputting your value back into the output field. All you're doing is returning the value. You need to set the value of your output field to "Generalities".
Example
var input = document.getElementById("onkeyup").value;
// You should be giving your elements meaningful IDs.
if(+input < 100) {
document.getElementById("output").value = 'Generalities';
// Assumes an output field called "output".
}
Try this:
function AnEventHasOccurred() {
var x = document.getElementById("onkeyup").value;
if (x < 100){
document.getElementById("eventlog").value = "Generalities";
}
}
I see a few errors. Check this out for comparison:
function AnEventHasOccurred() {
// should probably save the elements to variables
// since you'll be checking and changing the values
var x = document.getElementById("onkeyup");
var y = document.getElementById("eventlog");
// should be 100, not "100"
if (x.value < 100) {
y.value = "Generalities";
} else {
y.value = "";
}
}
This should work fine. Check it out on jsfiddle.
More Recommendations
Your return statement doesn't correspond with your "output": it
does nothing valuable in this case.
You check or set the value of an input by getting the element and
using its value key.
You should put semi-colons at the end of most javascript lines, with the exceptions generally being curly brackets {}, comments // and /* */, and empty lines.

Categories