Code Monkey home page Code Monkey logo

Comments (1)

nathan-abela avatar nathan-abela commented on May 24, 2024 3

Hi!

I do not have a YouTube channel which explains the solutions, however, below is the explanation to the JavaScript Certification Country Codes problem.

Code Explanation:

(1)

function fetch(url) {
    return new Promise((resolve, reject) => {
        https
            .get(url, (res) => {
                let result = '';
                res.on('data', (chunk) => {
                    result += chunk;
                });
                res.on('end', () => {
                    resolve(JSON.parse(result));
                });
            })
            .on('error', (error) => {
                reject(error);
            });
    });
}

Firstly, the res.on(data, callback) is a way of listening for the events emitted by a function, in this case, the https.get method. Since the https.get does not return a promise, the above code is wrapping that function to instead return a promise. Promises are explained more in-detail here.

A promise has additional methods, as opposed to using event emitters for every HTTPS request. So, instead of doing the below for every HTTPS request:

https
    .get('my-url', (res) => {
        let result = '';
        res.on('data', (chunk) => {
            result += chunk;
        });
        res.on('end', () => {
            const data = JSON.parse(result);
            // do something with the data
        });
    })
    .on('error', (error) => {
        // do something with the error
    });

This allows us to use a more streamlined approach to perform the HTTPS request. As that would instead allow us to run the below:

fetch('my-url')
    .then(data => {
        // handle the data
})
    .catch(error => {
        // handle the error
})

(2)

async function getCountryName(code) {
    const { total_pages, data } = await fetch(
        `https://jsonmock.hackerrank.com/api/countries?page=1`
    );
    const answer = findInCountries(data, code);
    if (answer) return answer.name;

    for (let i = 2; i <= total_pages; i++) {
        const { data } = await fetch(
            `https://jsonmock.hackerrank.com/api/countries?page=${i}`
        );
        const answer = findInCountries(data, code, i);
        if (answer) return answer.name;
    }
}

function findInCountries(countries, code, i = 1) {
    return countries.find((c) => c['alpha2Code'] === code);
}

The main principle of the above is that it uses Object Destructuring to get the data in JSON format for page 1 and tries to find the property alpha2Code that matches the code the user is searching for. If the result is not found, it will continue to search each page and returns the country name if it is found.

Note:

The above approach is only recommended for this solution, as the above could be simplified by using node-fetch, and end up doing the same in fewer lines of code, as seen below.

const fetch = require('node-fetch');

async function getData(url) {
    const res = await fetch(url);
    return res.json();
}

The await is being used to wait until the promise fetch(url) is settled (either resolved or rejected), then it would need to be a bit longer for error handling with a try-catch block, similar to the one mentioned at the end of (1).

I hope this was of help!

from hackerrank-solutions.

Related Issues (3)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.