JavaScript quiz questions

Hello everyone,

I’m new here and I came across this forum and seems to be very helpful when it comes down to asking questions about code. So, so currently I’m working on some made-up quizzes created by a developer. So here we go,

this was the initial quiz that i worked on…

apiResponse

{

  body: {

    testData: {

      testTime: 1590769283520,

        dn: 48.123,

          up: 24.765

    }

  }

}



// Instructions

// 1) create a method that takes the apiResponse as an input

// 2) method should have all contained logic in a try/catch

// 3) method should have proper error handling to not rely on try/catch

// 4) write just the 1 method, no helper methods (meaning this method contains all logic)

// 5) method should return the expected output structure with proper data, as provided below

// 6) notate each line of code to explain what it is doing

// 7) you can use a console log to test your method return to ensure it is working as expected.





successful response

{

  flowStatus: 'SUCCESS',

    flowStatusMessage: '',

      testTimeMilliseconds: 1590769283520,

        downStream: 48.123,

          upStream: 24.765

}



failed response

{

  flowStatus: 'FAILURE',

    flowStatusMessage: '' // this should contain a string that represents the error encountered

}

after working on this a bit, here is the result that I obtained and of course was tweaked by the developer…

parseResponse(apiResponse) {

  const finalObj = {}; // creating my initial empty object that I will add to as I evaluate the apiResponse. This obj is what I will return to the front end.

  try {

    // apiResponse is expected to be an object from the downstream system, so a standard truthy check will tell us it is not a falsey, so we assume if truthy it is an object.

    // the data I want is 2 levels in apiResponse, so I need to evaluate each level with a truthy to ensure neither nested level (which I expect to be objects) are a falsey.

    if (apiResponse && apiResponse.body && apiResponse.body.testData) {

      const testData = apiResponse.body.testData; // since I know I will need to evaluate multiple data points nested within the testData object, I am storing that object in a new temp variable to simplify the logic next.

      finalObj.flowStatus = 'SUCCES';

      finalObj.flowStatusMessage = ''; // not an error, so no message to provide

      finalObj.testTimeMilliseconds = testData.testTime ? testData.testTime : new Date().getTime(); // if testTime is not truthy, setting value to current time in milliseconds

      finalObj.downStream = testData.dn ? testData.dn : null; // if dn is not truthy, set value to null as I dont know actual speed test results

      finalObj.upStream = testData.up ? testData.up : null; // if dn is not truthy, set value to null as I dont know actual speed test results

    } else { // apiResponse or apiResponse.body or apiResponse.body.testData were not truthy, and thus I have no valid data to parse

      finalObj.flowStatus = 'FAILURE';

      finalObj.flowStatusMessage = 'Unable to parse API data'; // Do not have valid data to drive a message, have to provide a hardcoded error message

    }

    console.log('finalObj: ', finalObj);

  } catch (e) { // storing the error object provided to the catch in the variable e

    finalObj.flowStatus = 'FAILURE';

    finalObj.flowStatusMessage = e.message; // error object contains a message in a string that indicates what failed in the try

    console.log('finalObj: ', finalObj);

  }

};

the secondary code was the final outcome. However, I am now working on quiz #2.. here is what is being asked..

apiResponse1
{
  body: {
    testData: {
      liveTest: {
        testTime: 1590769283520,
        speedData: [
          { downLoad: 52.452 },
          { upLoad: 27.842 }
        ]
      },
      historicalTest: {
        testTime: 1490769283520,
        speedData: [
          { downLoad: 48.123 },
          { upload: 24.765 }
        ]
      }
    }
  }
}

apiResponse2
{
  body: {
    testData: {
      error: {
        errorCode: 503
        errorMesage: 'rg unreachable'
      }
    }
  }
}

  // Instructions
  // 1) Assume all the same instructions from quiz 1
  // 2) Method should properly handle both apiResponse 1 and apiResponse2
  // 3) Should not hardcode or use 'magic numbers' when it comes to parsing the api response
  // 4) Should provide expected output for a successful response and a failed response
// 5)  I only want you to evaluate the the live test results

  // successful response
  {
    flowStatus: 'SUCCESS',
    flowStatusMessage: '',
    testTimeMilliseconds: 1590769283520,
    downStream: 48.123,
    upStream: 24.765
  }

  // failed response
  {
    flowStatus: 'FAILURE',
    flowStatusMessage: '' // this should contain a string that represents the error encountered
  }

Then, here is the code that I have so far.

//const parseResponse = async (apiResponse1) => {
    let finalObj = {};
    try {
        if (apiResponse1 && apiResponse1.body && apiResponse1.body.testData && apiResponse1.body.testData.liveTest && apiResponse1.body.testData.liveTest.speedData) {
            const testData = apiResponse1.body.testData.liveTest;
            finalObj.flowStatus = 'SUCCESS';
            finalObj.flowStatusMessage = '';
            finalObj.testTimeMilliseconds = apiResponse1.body.testData.liveTest.testTime ? apiResponse1.body.testData.liveTest.testTime : null;
            finalObj.downStream = apiResponse1.body.testData.liveTest.speedData.downLoad ? apiResponse1.body.testData.historicalTest.speedData.downLoad : null; // if dn is not truthy, sends call to apiResponse2 for the error
            finalObj.upStream = apiResponse1.body.testData.liveTest.speedData.upLoad ? apiResponse1.body.testData.historicalTest.speedData.upLoad : null; // if dn is not truthy, sends call to apiResponse2 for the error
        } else if (apiResponse2 && apiResponse2.body && apiResponse2.body.testData) { //if fails, the calls for apiResponse two but also checks if it's truthy.
            finalObj.flowStatus = 'FAILURE';
            finalObj.flowStatusMessage = 'Unable to Parse API';
        }
        console.log('finalObj: ', finalObj);
    } catch (e) {
        finalObj.flowStatus = 'FAILURE';
        finalObj.flowStatusMessage = e.message; // error object contains a message in a string that indicates what failed in the try
        console.log('finalObj: ', finalObj);
    }
//};

in the above code, I commented out the arrow function because it wasn't giving me anything in the console to make sure it's right. I don't feel im doing this entirely correctly.  So for the second apiResponse two, I'm assuming we need to make a secondary call to that API if we cannot pull speed tests.