call api

by oktaviardi pratama

JavaScript

function test(date){
// write your code here
    // API endpoint: https://jsonmock.hackerrank.com/api/stocks?date=<date>
    let url = `https://jsonmock.hackerrank.com/api/stocks?date=${date}`
    let result = null
    // let xhr = new XMLHttpRequest();
    // xhr.open('GET',url, true);
    // xhr.onreadystatechange = function() {
    //     if(xhr.readyState === XMLHttpRequest.DONE && xhr.status === 200) {
    //         result = JSON.parse(xhr.responseText)
    //     }
    // }
    fetch(url)
    .then(res => res.json())
    .then(data => {
    		console.log(data.data)
        result = data.data
    })
    .catch(err => {
     console.log('err',err)
    })
    return result
}

console.log(test('5-January-2000'))

/* var xhr = new XMLHttpRequest();
xhr.open("GET", `https://jsonmock.hackerrank.com/api/stocks?date=5-January-2000`, true);
xhr.onreadystatechange = function() {
  if (xhr.readyState === XMLHttpRequest.DONE && xhr.status === 200) {
    console.log(JSON.parse(xhr.responseText));
  }
};
xhr.send(); */