We help IT Professionals succeed at work.
Get Started

Curious how Expert Awards are earned? Find out on this week's podcast, featuring Experts Exchange CEO Randy Redberg.Listen Now

x
Troubleshooting Question

How do I convert this code to if-else statements?

SoLost
SoLost asked
on
34 Views
Last Modified: 2020-11-28
I'm trying to modify some javascript written by another developer.  Unfortunately I can't work out the logic.  I was wondering if someone could help me convert this function to if-else statements.

Essentially the code is going through a group of fields and updating the output based on a set of conditions.

Here is the original function:
function (e) {
    o.fields.numberdisplay && (o.fields.numberdisplay.value = "" === e.numberfull ? e.numberdisplay : e.numberfull),
    o.fields.address && (o.fields.address.value = e.displayline),
    o.fields.suburb && (o.fields.suburb.value = "" === e.suburb ? "" === e.mailtown ? e.city : e.mailtown : e.suburb),
    o.fields.city && (o.fields.city.value = "" === e.mailtown ? e.city : e.mailtown),
    o.fields.territory && (o.fields.territory.value = e.territory),
    o.fields.x && (o.fields.x.value = e.x),
    o.fields.y && (o.fields.y.value = e.y),
    o.fields.dpid && (o.fields.dpid.value = e.dpid),
    o.fields.id && (o.fields.id.value = e.id),
    o.fields.postcode && (o.fields.postcode.value = e.postcode),
    o.fields.rdnumber && (o.fields.rdnumber.value = e.rdnumber),
    o.fields.line1 && (o.fields.line1.value = e.address1),
    o.fields.line2 && (o.fields.line2.value = e.address2),
    o.fields.line3 && (o.fields.line3.value = e.address3),
    o.fields.line4 && (o.fields.line4.value = e.address4),
    o.fields.city || !o.fields.suburb || "" !== e.suburb || "" === e.city && "" === e.mailtown || (o.fields.suburb.value = "" === e.mailtown ? e.city : e.mailtown),
    o.fields.address1 && o.fields.address2 ? (e.address4 || 0 === e.address2.indexOf("RD ") ? (o.fields.address1.value = e.address1,
        o.fields.address2.value = e.address2) : (o.fields.address1.value = e.displayline,
            o.fields.address2.value = ""),
        !o.fields.suburb && e.suburb && "" !== e.suburb && ("" !== o.fields.address2.value && (o.fields.address1.value += ", " + o.fields.address2.value),
            o.fields.address2.value = e.suburb)) : o.fields.address1 && !o.fields.address2 && (o.fields.address1.value = e.displayline)
}

And below is what I've hopefully worked out correctly so far (apart from the last ones):
function (e) {
    if (o.fields.numberdisplay) o.fields.numberdisplay.value = e.numberfull === '' ? e.numberdisplay : e.numberfull;
    if (o.fields.address) o.fields.address.value = e.displayline;
    if (o.fields.suburb) o.fields.suburb.value = e.suburb === '' ? e.mailtown === '' ? e.city : e.mailtown : e.suburb;
    if (o.fields.city) o.fields.city.value = e.mailtown === '' ? e.city : e.mailtown;
    if (o.fields.territory) o.fields.territory.value = e.territory;
    if (o.fields.x) o.fields.x.value = e.x;
    if (o.fields.y) o.fields.y.value = e.y;
    if (o.fields.dpid) o.fields.dpid.value = e.dpid;
    if (o.fields.id) o.fields.id.value = e.id;
    if (o.fields.postcode) o.fields.postcode.value = e.postcode;
    if (o.fields.rdnumber) o.fields.rdnumber.value = e.rdnumber;
    if (o.fields.line1) o.fields.line1.value = e.address1;
    if (o.fields.line2) o.fields.line2.value = e.address2;
    if (o.fields.line3) o.fields.line3.value = e.address3;
    if (o.fields.line4) o.fields.line4.value = e.address4;
    
    // These are the ones that I'm unsure of.
    
    o.fields.city || !o.fields.suburb || "" !== e.suburb || "" === e.city && "" === e.mailtown || (o.fields.suburb.value = "" === e.mailtown ? e.city : e.mailtown),
    o.fields.address1 && o.fields.address2 ? (e.address4 || 0 === e.address2.indexOf("RD ") ? (o.fields.address1.value = e.address1,
        o.fields.address2.value = e.address2) : (o.fields.address1.value = e.displayline,
            o.fields.address2.value = ""),
        !o.fields.suburb && e.suburb && "" !== e.suburb && ("" !== o.fields.address2.value && (o.fields.address1.value += ", " + o.fields.address2.value),
            o.fields.address2.value = e.suburb)) : o.fields.address1 && !o.fields.address2 && (o.fields.address1.value = e.displayline)
}
First, can you tell me if I've got the first ones correct, and secondly, help me figure out how to convert the last ones to if-else statements.

Thanks

Comment
Watch Question
CERTIFIED EXPERT
Most Valuable Expert 2020
Most Valuable Expert 2018
Commented:
This problem has been solved!
Unlock 1 Answer and 7 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant

An Experts Exchange subscription includes unlimited access to online courses.

Get Started
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE