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.
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));
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]];
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.
I need this comparison in my javascript to work.
if ((q2 != '' && correct2 != 'True') || (q2 != '' && correct2 != 'true') || (q2 != '' && correct2 != 'false') || (q2 != '' && correct2 != 'False'))
{
alert("You must enter true or false.");
}
q2 and correct2 are textboxes and if q2 has something in it and correct2 doesn't equal true, True, false, or False then I want the message box pops up. My code is not working. If I put true or false in the blank the error message still shows up.
EDIT
I have found how to make it work. Instead of putting || between the comparisons I put && and it works perfectly.
To get the value of a text input, do something like
var q2Value = q2.value;
For the conditionals, || is or, not or, and everytime you do a comparison you need 2 values/variables
(correct2 !== 'True ) || (correct2 !== 'true')...
You can see I wrapped the comparisons in parenthesis so its perfectly clear what should be compared to what, even if it isn't strictly necessary.
Since you need q2 to be correct before comparing the other condition, you can use a nifty feature called short-circuiting. Basically, && only proceeds if the first comparison is truthy, so you would do
(qa2 !== '') && (the rest)
Note that if a user doesn't enter the value at all, when you get the value of the text field it will be undefined (I think), not '''. So you should really just do
qa2 && (...)
Basically null, undefined, and '' are all falsy in javascript, so if qa2 is any of those values, the second part of the and won't be processed.
This should work.
if ((q2 != '' && correct2 != 'True') || (q2 != '' && correct2 != 'true') || (q2 != '' && correct2 != 'false') || (q2 != '' && correct2 != 'False'))
{
alert("You must enter true or false.");
}
try this
if(q2 !== '' && !(correct2 == 'true') && !(correct2 == 'True') && !(correct2 == 'False') && !(correct2 == 'false'))
{
alert("You must enter true or false.")
};
I need to check to see if a variable is null or has all empty spaces or is just blank ("").
I have the following, but it is not working:
var addr;
addr = " ";
if (!addr) {
// pull error
}
If I do the following, it works:
if (addr) {
}
What I need is something like the C# method String.IsNullOrWhiteSpace(value).
A non-jQuery solution that more closely mimics IsNullOrWhiteSpace, but to detect null, empty or all-spaces only:
function isEmptyOrSpaces(str){
return str === null || str.match(/^ *$/) !== null;
}
...then:
var addr = ' ';
if(isEmptyOrSpaces(addr)){
// error
}
* EDIT *
Please note that op specifically states:
I need to check to see if a var is null or has any empty spaces or for that matter just blank.
So while yes, "white space" encompasses more than null, spaces or blank my answer is intended to answer op's specific question. This is important because op may NOT want to catch things like tabs, for example.
if (addr == null || addr.trim() === ''){
//...
}
A null comparison will also catch undefined. If you want false to pass too, use !addr. For backwards browser compatibility swap addr.trim() for $.trim(addr).
You can use if(addr && (addr = $.trim(addr)))
This has the advantage of actually removing any outer whitespace from addr instead of just ignoring it when performing the check.
Reference: http://api.jquery.com/jQuery.trim/
Old question, but I think it deservers a simpler answer.
You can simply do:
var addr = " ";
if (addr && addr.trim()) {
console.log("I'm not null, nor undefined, nor empty string, nor string composed of whitespace only.");
}
Simplified version of the above: (from here: https://stackoverflow.com/a/32800728/47226)
function isNullOrWhitespace( input ) {
return !input || !input.trim();
}
You can create your own method Equivalent to
String.IsNullOrWhiteSpace(value)
function IsNullOrWhiteSpace( value) {
if (value== null) return true;
return value.replace(/\s/g, '').length == 0;
}
isEmptyOrSpaces(str){
return !str || str.trim() === '';
}
When checking for white space the c# method uses the Unicode standard. White space includes spaces, tabs, carriage returns and many other non-printing character codes. So you are better of using:
function isNullOrWhiteSpace(str){
return str == null || str.replace(/\s/g, '').length < 1;
}
isEmptyOrSpaces(str){
return str === null || str.trim().length>0;
}
Try this out
/**
* Checks the string if undefined, null, not typeof string, empty or space(s)
* #param {any} str string to be evaluated
* #returns {boolean} the evaluated result
*/
function isStringNullOrWhiteSpace(str) {
return str === undefined || str === null
|| typeof str !== 'string'
|| str.match(/^ *$/) !== null;
}
You can use it like this
isStringNullOrWhiteSpace('Your String');
function isEmptyOrSpaces(str){
return str === null || str.match(/^[\s\n\r]*$/) !== null;
}
I use simply this and this works for me most of the time.
it first trim the white spaces and then checks the length.
if(value.trim().length === 0)
{
//code for empty value
}
Maybe it's easier this way
if (!addr?.trim()){
//error
}
Based on Madbreaks' answer, and I wanted to account for undefined as well:
function isNullOrWhitespace(str) {
return str == null || str.match(/^\s*$/) !== null;
}
Jest tests:
it('works for undefined', () => {
expect(isNullOrWhitespace(undefined)).toEqual(true);
});
it('works for null', () => {
expect(isNullOrWhitespace(null)).toEqual(true);
});
it('works for empty', () => {
expect(isNullOrWhitespace('')).toEqual(true);
});
it('works for whitespace', () => {
expect(isNullOrWhitespace(' ')).toEqual(true);
// Tab
expect(isNullOrWhitespace(' ')).toEqual(true);
});
You can try this:
do {
var op = prompt("please input operatot \n you most select one of * - / * ")
} while (typeof op == "object" || op == "");
// execute block of code when click on cancle or ok whthout input