-3

first post and feeling a little lost at this point. I am trying to reproduce the result of Null in my json object. Currently the results produce the following:

{ overlapping_tradelines: false, time_for_overlap_months: 628, minimum_two_years_overlapping: false, version: '08.23.21' }

I would like for it to produce:

{ overlapping_tradelines: false, time_for_overlap_months: Null, minimum_two_years_overlapping: false, version: '08.23.21' }

I have tried to implement a second if statement with no avail. Would I need to implement the else if in order to get the results that I want to achieve? Here is my working code: *note the xml paths have been removed for ease of code. let me know if you would like the files to dig deeper :)

const jsonOutput = {};
let isOverLap = false;
let openDate = null;
let minimum_two_years_overlapping = false;

var stop = false;

for (let i = 1; i <= applicantSegLen; i++) {
  if (stop == true) break;
  const account = xpath.select("string(//TRsegments[1]/TRsegment[" + i + "]/@account)", doc);
  const dateopen = xpath.select("string(//TRsegments[1]/TRsegment[" + i + "]/@dateopen)", doc);
  const balance = xpath.select("string(//TRsegments[1]/TRsegment[" + i + "]/@balance)", doc);

  for (let j = 1; j <= coApplicantSegLen; j++) {
    if (stop == true) break;
    const account2 = xpath.select("string(//TRsegments[1]/TRsegment[" + j + "]/@account)", coDoc);
    const dateopen2 = xpath.select("string(//TRsegments[1]/TRsegment[" + j + "]/@dateopen)", coDoc);
    const balance2 = xpath.select("string(//TRsegments[1]/TRsegment[" + j + "]/@balance)", coDoc);

    if (account === account2 && dateopen === dateopen2 && balance === balance2) {
      openDate = dateopen;
      isOverLap = true;
      stop = true;
    }
    }
  }


//diff is the conversion for opendate to today's date. It converts milliseconds to months
jsonOutput["overlapping_tradelines"] = isOverLap;
const now = Date.now();
openDate = new Date(openDate);

let diff = now - openDate;
diff = diff / (30 * 24 * 60 * 60 * 1000);
const months = Math.floor(diff);

jsonOutput["time_for_overlap_months"] = months;
minimum_two_years_overlapping = diff == 24;
jsonOutput["minimum_two_years_overlapping"] = minimum_two_years_overlapping;

let version = "08.23.21";
jsonOutput["version"] = version;

console.log(jsonOutput);
  • What do you expect the statements `jsonOutput["time_for_overlap_months"] === null;` and `jsonOutput["minimum_two_years_overlapping"] === false;` to do? Please see [In javascript == vs =?](/q/11871616/4642212). – Sebastian Simon Aug 24 '21 at 22:16
  • I would like those statements to be displayed in that manner, if "overlapping_tradelines" equals false. – Angelo G. Vazquez Aug 24 '21 at 22:32
  • `["overlapping_tradelines"] === false` is always `false`. `"overlapping_tradelines" === true` is always `false`. _“if "overlapping_tradelines" equals false”_, then _what_? These statements do nothing. Your entire lower `if` statement is never executed. – Sebastian Simon Aug 24 '21 at 22:37
  • I see ok that is something that I added thinking it would be the solution. The entire lower ```if``` statement non-existent with my original code. Is there a different approach to achieve the result im looking for? – Angelo G. Vazquez Aug 24 '21 at 22:47
  • All you need to do is set `jsonOutput["time_for_overlap_months"] = isOverlap ? months : null` [docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator) – Charlie Bamford Aug 24 '21 at 23:09
  • @Charles Bamford I knew it would be a simple if else with ternary. What a great suggestion. Thank you and the results are producing accordingly. Thank you!!! – Angelo G. Vazquez Aug 24 '21 at 23:30

1 Answers1

0

Here is the updated response with @Charles Bamford's suggestion with the addition of the ternary if/else:

jsonOutput["overlapping_tradelines"] = isOverLap;
const now = Date.now();
openDate = new Date(openDate);

let diff = now - openDate;
diff = diff / (30 * 24 * 60 * 60 * 1000);
const months = Math.floor(diff);
jsonOutput["time_for_overlap_months"] = isOverLap ? months : null

minimum_two_years_overlapping = diff == 24;
jsonOutput["minimum_two_years_overlapping"] = minimum_two_years_overlapping;

let version = "08.23.21";
jsonOutput["version"] = version;

console.log(jsonOutput);