Reputation: 1678
Is it possible to further simplify the following javascript
statement. the requirement is if any input is negative it should execute the statement inside if block. i am wondering if i can avoid < 0
multiple times and just use once
if (Number(cash.value) < 0 || Number(cheque.value) < 0 || Number(charge.value) < 0 || Number(card.value) < 0) {
doSomething();
}
i simplified to this logic
if ((Number(Cash.value) || Number(Cheque.value) || Number(Charge.value) || Number(Card.value)) < 0)
but it does not work and does not meet requirement. the above logic works but there is duplication of <0 logic
Upvotes: 0
Views: 120
Reputation:
You can simplify removing for example Number()
- the + sign turns strings holding a number into an actual number, and we can still use the faster logic operands:
if (+cash.value < 0 || +cheque.value < 0 || +charge.value < 0 || +card.value) < 0) {
doSomething();
}
As to the problem with the second line -
The reason this line doesn't work (besides from a possible typo, as JavaScript is case-sensitive):
if ((Number(Cash.value) || Number(Cheque.value) || Number(Charge.value) || Number(Card.value)) < 0)
is that this Number(Cash.value)
is converted into a boolean value, ie. if the value is 0 then false, anything else will convert to true. So if the numbers are non-0, plus or minus, they will pass through, except the last test which only passes as intended. Each value has to be tested individually.
Word of advice: unless you need to check numerous values in parallel, avoid using arrays (whether its ES6 or earlier). This is significantly slower than doing boolean checks directly, especially in older browsers which parses and have no optimization of JavaScript.
Upvotes: 2
Reputation: 68685
You can try to do like this
var numbers = [Cash.value, Cheque.value, Charge.value, Card.value];
if(numbers.some(value => Number(value) < 0)){
doSomething();
}
For Example, is working in ES6
var numbers = [11, 12, -1,-7];
if(numbers.some(value => value < 0)){
console.log('Found !!!');
}
For under IE 9, you can write a function and use
function anyNegative(numbers){
for(var i = 0; i < numbers.length; i++){
if(numbers[i] < 0){
return true;
}
}
return false;
}
var numbers = [11, 12, -1,-7];
if(anyNegative(numbers)){
console.log('Found !!!');
}
Upvotes: 6
Reputation: 270
How about :
if([Cash,Cheque,Charge,Card].some(obj => Number(obj.value) < 0)) {
doSomething();
}
Upvotes: 0
Reputation: 670
I think, not really.
Your second statement will not work, because you are essentially checking all the values - except for the last one - to be truthy, and then only the last one for being negative.
If you want to make the code more readable, you might add a helper function:
function isNegative(value) {
return Number(value) < 0;
}
And then use that in your if statements.
Technically you could make a function like this:
function atLeastOneNegative() {
for (var i = 0; i < arguments.length; i++) {
if (Number(i) < 0) return true;
}
return false;
}
But I think the first option would give nicer code.
Upvotes: 0
Reputation: 413
Or have it as a function, in case you need it in more places. It takes N number of inputs to verify
https://jsfiddle.net/2L1L7ncj/
function isAnyArgumentNegative(){
var args = Array.prototype.slice.call(arguments);
return args.some(function(a){
return Number(a) < 0;
});
}
Upvotes: 0