mahnoorfatima / Assignment-WebScrapper

0 stars 0 forks source link

code reuse? #4

Open neebz opened 4 years ago

neebz commented 4 years ago

https://github.com/mahnoorfatima/Assignment-WebScrapper/blob/ec53d357002a0798e84cdf7cbdd3d3e37b3b0ff4/controller.js#L18

This code inside else and the if is pretty much same except for the for loop. can you think of a way to reuse the code?

mahnoorfatima commented 4 years ago

Yes, if we push the single element to array and then for each element, call the requestTitle().But it will come with the cost of time complexity as even for a single element in array , it will go into for loop. I have chosen this solution , "if" concludes if the given parameters are in array , then for each element(param) in array call the function requestTitle() where as for a single param, just call requestTitle() not to trade time complexity.

mahnoorfatima commented 4 years ago
const values= Array.isArray(request.query.address) ? request.query.address: [request.query.address] || [ ];
    var arrayLength = values.length;
    for (var counter = 0; counter < arrayLength; counter++) {
    helper.requestTitle(values[counter], (title) => {   
        responseHelper.sendDescription(response, title);

    });}