Can we write an if statement backwards in javascript? [closed] - javascript

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
What is the difference between these two expressions?
if (userStatus === 'OFFLINE') document.forms.newMessage.fields.disabled = true;
and
document.forms.newMessage.fields.disabled = (userStatus === 'OFFLINE');

document.forms.newMessage.fields.disabled = (userStatus === 'OFFLINE')
is equivalent to
if (userStatus === 'OFFLINE') {
document.forms.newMessage.fields.disabled = true;
} else {
document.forms.newMessage.fields.disabled = false;
}
Notice the else statement. If there is no else statement then your first piece of code only sets document.forms.newMessage.fields.disabled when the condition (userStatus === 'OFFLINE') is met.
Consider the case in which
document.forms.newMessage.fields.disabled is already true but userStatus is "ONLINE".
With your first piece of code, document.forms.newMessage.fields.disabled will remain true (because of the lack of else statement) , but with the second piece of code, it will be set to false.

Related

How can filter string that don't match in totally? [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
I want to filter string that not have all coincidences (I deleted blank spaces)
With string.includes()
'videocardgigabytegeforce3070'.includes('videocardgigabyte') return true
'videocardgigabytegeforce3070'.includes('videocardgeforce') return false
I want to second case also return true, If you have a solution with function or regex I'll appreciate it
const str = 'videocardgigabytegeforce3070';
const regex = /videocard.*geforce/;
const result = regex.test(str);
console.log(result); // true
or
const str = 'videocardgigabytegeforce3070';
const regex = /videocard.*geforce/;
const result = str.match(regex);
if (result && result.length > 0) {
console.log(true); // true
} else {
console.log(false);
}

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.

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

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).

How to restore sentence in javascript [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 8 years ago.
Improve this question
I have this
var input = "([lazy({(jumps{fox([quick(The)]brown)})over}the)]dog)";
I want to get
The quick brown fox jumps over the lazy dog.
Any ideas? I tried to use RegEx but can not find.
We need to have stack kind of approach with the array here. See the below implementation.
var res = "([lazy({(jumps{fox([quick(The)]brown)})over}the)]dog)".split("");
var txt = [],lvl=-1;
res.forEach(function(e,i){
if(e=='('||e=='{'||e=='['){
lvl++;
} else if(e==')'||e=='}'||e==']'){
lvl--;
} else {
if(typeof txt[lvl]=='undefined'){
txt[lvl] = e;
} else {
txt[lvl] = txt[lvl] + e;
}
}
});
txt = txt.reverse().join(" ");
console.log(txt);
if(lvl!=-1) {
//this will alert if any missing parenthesis
alert("Pattern error in input");
}
Edit : updated as per Question owner's description of the pattern in input.

Categories