Multiple conditions with logical AND operator in JavaScript [closed] - javascript

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;
}

Related

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 : ''
}

Javascript execute a function of the number of indexes in two arrays are equal? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I'm trying to execute a function when two arrays have an equal length that is greater than zero.
My program assigns the class of 'letter' to each list item in the phrase div. If a button press matches a letter in that div, that letter gets assigned the class 'show' which reveals it to the user.
I don't understand why my win condition isn't being met. Is there something wrong with the way I'm comparing the length of the two indexes?
I've included my codePen here: https://codepen.io/Azo3307/pen/vPjwxr
My checkWin() function is on line 100, and then it is called on line 143 after each button press is triggered.
// Check win condition
function checkWin() {
let showClass = document.getElementsByClassName('show');
let lettersClass = document.getElementsByClassName('letters');
if (showClass.length == lettersClass.length && showClass.length > 0 && lettersClass.length > 0) {
console.log('you win');
addElement('win', `You Win!`, `"${phraseArray}" is correct! `);
} else if (missed == 5) {
console.log('you lose');
addElement('lose', 'Game Over!', 'Better luck next time!');
}
}
Any help is much appreciated. Thank you.
This is because you're asking for let document.getElementsByClassName('letters') instead of letter:
<li class="letter show">t</li>
Second point, if showClass.length == lettersClass.length and showClass.length > 0, you don't need to test lettersClass.length > 0 ;)
function checkWin() {
let showClass = document.getElementsByClassName('show');
let lettersClass = document.getElementsByClassName('letter');
if (showClass.length == lettersClass.length && showClass.length > 0) {
console.log('you win');
addElement('win', `You Win!`, `"${phraseArray}" is correct! `);
} else if (missed == 5) {
console.log('you lose');
addElement('lose', 'Game Over!', 'Better luck next time!');
}
}
Last one, it's easy to know the text before trying anything just by selecting all the block ;) You should display:none instead of put it transparent.
Line 102:
let lettersClass = document.getElementsByClassName('letters');
rename to
let lettersClass = document.getElementsByClassName('letter');
Looks like just small typo. Btw code looks great, thumbs up.

How to check for two conditions using && operator [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 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).

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