I've the following code
var oDataEn = aData[0][aProperties[0].split('/')[0]];
if (!(oDataEn != null && oDataEn.results && oDataEn.results.length > 0)) {
...
else
...
This is working OK except when
aData[0] = 'undefined'
my question is if there a better way to write it instead of just adding before
if(aData[0] != null)
{
var oDataEn = aData[0][aProperties[0].split('/')[0]];
}
I dont want to have two if's if possible...
Start with a ternary - I assume aData[0] may be falsy (null, undefined, 0 or ""):
var oDataEn = aData[0]?aData[0][aProperties[0].split('/')[0]]:null;
if (oDataEn && oDataEn.results && oDataEn.results.length > 0) {
if(aData[0] === undefined){}
is the best way so far I know.
As per your comment
if(typeof aData[0] != 'undefined' /*&& other if statements*/){}
So it will not process if it is undefined.
By using ternary operator u can do this. Ternary operator will reduce the line of codes only.
var oDataEn = aData[0] === undefined ? '' : aData[0][aProperties[0].split('/')[0]];
Related
I am validating my time in this way
if (
timeInMins != 0 &&
timeInMins != "0" &&
timeInMins != undefined &&
timeInMins != "undefined" &&
timeInMins != null &&
timeInMins != "" &&
!isNaN(timeInMins)
) {
timeInMinsCumulative += parseFloat(timeInMins);
}
Is there any way to make this ugly if-check to sophisticated code?
There are 6 falsy values in javascript: undefined, null, NaN, 0, "" (empty string), and false of course.
So, you can just write
if (timeInMins && timeInMin !== '0') {
timeInMinsCumulative += parseFloat(timeInMins);
}
This uses the coercion behavior of JavaScript and the logical AND operator to simplify your code. The following is very nearly equivalent to your code, but it will also guard against the arguments false and 0n.
if (timeInMins &&
timeInMins !== '0' &&
timeInMins !== 'undefined') {
// whatever
}
Questions for you: do you really expect to ever get the string 'undefined' passed to you? Why do you want to guard against '0' being sent to parseFloat? Are you sure parseInt is not what you want?
It seems you want to check if timeInMins is precise Number type or not.
function isValidNumber(num) {
return typeof num === "number" && !isNaN(num);
}
console.log(isValidNumber(""));
console.log(isValidNumber(undefined));
console.log(isValidNumber(NaN));
console.log(isValidNumber("undefined"));
console.log(isValidNumber(true));
console.log(isValidNumber(false));
console.log(isValidNumber(0));
console.log(isValidNumber("0"));
console.log(isValidNumber(1.234));
This is apex code, but for the propose seems to work like in Java or JScript.
I've the following line of code (comment included):
// If GPMIR_fld_codigoOrigen__c 09 Fotofactura will not be assigned to agency for send csv
eLead.GPMIR_fld_assignAgency__c = (eLead.GPMIR_fld_codigoOrigen__c!='09') ? ((u.Contact != null && u.Contact.Account != null && rt == 'Agencia') ? u.Contact.Account.Id : null) : null;
Trying to translate that to regular if-else what i think it's happening here is (comment included):
// If GPMIR_fld_codigoOrigen__c != 09 then GPMIR_fld_assignAgency__c == null
if(eLead.GPMIR_fld_codigoOrigen__c!='09'){
if(u.Contact != null && u.Contact.Account != null && rt == 'Agencia'){
eLead.GPMIR_fld_assignAgency__c = u.Contact.Account.Id;
}else{
eLead.GPMIR_fld_assignAgency__c = null;
}
}else{
eLead.GPMIR_fld_assignAgency__c = null;
}
Probably i'm wrong but if anyone could help me to do the proper translation i would really appreciate it
Seems correct, but You can also do this inside a function
Public string MyFunction(){
if(eLead.GPMIR_fld_codigoOrigen__c!='09'){
if(u.Contact != null && u.Contact.Account != null && rt == 'Agencia'){
Return u.Contact.Account.Id;
}
}
Return null;
}```
//Then in main(), eLead.GPMIR_fld_assignAgency__c = MyFunction()
Hopefully I'm right as well.
Not sure why this only seems to work with the response "yes"(or "Yes"/"YES") - any help would be appreciated. Pretty new to this!
var yesNo = window.prompt("Are you from Earth?");
var lowerYesNo = yesNo.toLowerCase();
while (lowerYesNo != ("yes" || "no")){
yesNo = window.prompt("Please answer with \"Yes\" or \"No\". Are you from Earth?");
lowerYesNo = yesNo.toLowerCase();
}
switch (lowerYesNo){
case "yes":
window.alert("I thought so!");
break;
case "no":
window.alert("Capture the alien!!!");
break;
default:
window.alert("I don't know how you got this to display!");
}
"yes" || "no"
returns "yes", because it is a "truthy" value and therefore the first operand of || is returned.
If you want to compare to both values, you will need to split this into two separate comparisons:
while (lowerYesNo != "yes" && lowerYesNo != "no")) {
//...
The correct way to write that is
while (lowerYesNo !== 'yes' && lowerYesNo !== 'no') {
// ...
}
you can try this,
((lowerYesNo != "yes") && (lowerYesNo != "no")){
what you probably wanted to do was:
while (!(lowerYesNo == "yes" || lowerYesNo == "no")){
...
that's same as
while (lowerYesNo != "yes" && lowerYesNo != "no"){
...
Meanwhile, take note:
(x || y) // this expression will return x, if x is truthy otherwise y
and
(x && y) // this expression will return x, if x is falsy, otherwise it returns y
Hence the loop in your question is equivalent to this:
while (lowerYesNo != "yes"){
... // you must say "yes" else you're stuck in here.
}
Yes it can be but it needs to be a whole expression
while( expression1 || expression2)
So in your case it would be
while (lowerYesNo != "yes" || lowerYesNo != "no")
though as has been pointed out in this instance you probably want to use an &&, since you are trying to print the message only if it is (NOT yes AND NOT No)
Because evaluation of the expression
"yes" || "no"
results in
"yes"
according to javascript rules in evaluating the "or" operator.
I am trying to write a loop to check whether a card number has the right format but I can not get it to work and debugging is just not working.
I need to check for the format: XXX-XXX-XXX1
Here is my javascript:
if(pieces[2] == ''){
$('#hcc_validated').value = 'incorrect';
showCardError();
$('#fname').value = '3';
}else if((pieces[0].length == 3 && typeof pieces[0] == "string") && (pieces[1].length == 3 && typeof pieces[1] == "string") && (thirdp.length == 3 && typeof thirdp == "string") && (fourthp.length == 1 && typeof fourthp == "number")){
$('#hcc_validated').value = 'validated';
showCardError();
$('#fname').value = '2';
}else{
$('#hcc_validated').value = 'not validated';
showCardError();
$('#fname').value = '1';
}
Can anyone see where I am going wrong? (This is just part of the code but I know for sure that this is where I am going wrong as the rest of the code works fine...
Here is my JSFiddle if you want full code:
JSFIddle Example
Thanks guys
This would be better solved using regular expressions.
Specifically;
[0-9]{3}-[0-9]{3}-[0-9]{3}1
Javascript:
var inputstring = "123-123-1231";
var pattern = new RegExp("[0-9]{3}-[0-9]{3}-[0-9]{3}1");
var result = pattern.test(inputstring);
// now result contains true or false
Given that you have:
> var fourthp = pieces[2].slice(-1);
then it is pretty much guaranteed that it's a string, so when will
> (fourthp.length == 1 && typeof fourthp == "number")
ever be true? Even if it's a Number, then fourthp.length will be undefined, so it will always be false either way.
Code:
if (!IDTextField.value && !FirstNameField.value &&
!LastNameField.value && !DateOfBirthField.value!GenderField.value) {
alert('No criteria Added');
return;
}
The alert is not called when all the text fields are blank.
You're missing the && between the last two criteria
It should be:
if (!IDTextField.value && !FirstNameField.value &&
!LastNameField.value && !DateOfBirthField.value && !GenderField.value)
In cases like this, it makes a lot of sense to format your if statement like this:
if ( !IDTextField.value
&& !FirstNameField.value
&& !LastNameField.value
&& !DateOfBirthField.value
&& !GenderField.value)
If you do it this way, you can't make the mistake you just made.
xbonez got it right. You are missing && between last two expression.
For something not so important, I would like to get all expressions evaluated using || and then add negation using !, rather than negating all expression and evaluate them with &&. This can make this expression a little faster, if am not wrong.
if (!(IDTextField.value || FirstNameField.value ||
LastNameField.value || DateOfBirthField.value || GenderField.value)) {
alert('No criteria Added');
return;
}
Tell me what you all think??
What is this little abomination?
... !DateOfBirthField.value!GenderField.value
I think that should be:
... !DateOfBirthField.value && !GenderField.value
You should write your code like this
if (!IDTextField.value && !FirstNameField.value &&
!LastNameField.value && !DateOfBirthField.value && !GenderField.value) {
alert('No criteria Added');
return;
}
You're missing the && between the last two criteria
the && is missed ,add it and try ,should work if no other errors exist