This JavaScript code is designed to interact with an API to retrieve the number of transactions associated with a given username. It uses the axios library to make HTTP requests and the fs library to write the result to a file. The script reads the username from the standard input, makes a GET request to the API to retrieve the user's ID, and then makes another GET request to retrieve the total number of transactions for that user.
CodeRankGPT is a tool powered by GPT-4 that quietly assists you during your coding interview, providing the solutions you need.
In real-time and absolutely undetectable 🥷
The script uses the axios library to make asynchronous HTTP requests to the API. It first retrieves the user's ID by making a GET request to the 'article_users' endpoint with the username as a parameter. If the user exists, it then makes a second GET request to the 'transactions' endpoint with the user's ID as a parameter to retrieve the total number of transactions. The result is then written to a file using the fs library. If the user does not exist, the script returns a 'Username Not Found' message.
use strict';
const fs = require('fs');
const https = require('https');
process.stdin.resume();
process.stdin.setEncoding('utf-8');
let inputString = '';
let currentLine = 0;
process.stdin.on('data', function(inputStdin) {
inputString += inputStdin;
});
process.stdin.on('end', function() {
inputString = inputString.split('\n');
main();
});
function readLine() {
return inputString[currentLine++];
}
const axios = require('axios');
async function getNumTransactions(username) {
try {
const {data} = await axios.get(`https://jsonmock.hackerrank.com/api/article_users?username=${username}`);
if(data.data && data.data.length !==0){
const userID = data.data[0].id;
const response = await axios.get(`https://jsonmock.hackerrank.com/api/transactions?&userId=${userID}`)
return response.data.total;
} else {
return 'Username Not Found';
}
} catch (error){
console.log(error);
}
}
async function main() {
const ws = fs.createWriteStream(process.env.OUTPUT_PATH);
const username = readLine().trim();
const result = await getNumTransactions(username);
ws.write(result.toString());
}
If you have a HackerRank Certification test or a coding interview coming up, you can use CodeRankGPT to your advantage. It will assist you during your test and help ensure you get the certification or the job.
AI is here now, and other candidates might be using it to get ahead and win the job. 🧐
The form has been successfully submitted.