Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Can someone please help me solve this problem in Javscript please? Thanks 10. A

ID: 3746785 • Letter: C

Question

Can someone please help me solve this problem in Javscript please? Thanks

10. A function that returns a promise for a random name from the uinames API. Pass the URL query parameters in an object. The only parameters your function are to accept are gender and region. Do not just pass all paramters from your caller through to uinames. Do pass the value 1 to uinames for the API parameter amount. Simply for practice, your promise should with resolve successfully with a string of the form 'surname, name or reject if there are any problems. (Let the rejection happen naturally with whatever the module request- promise does. You'll get an object with a message field that has a response code and message from the API provides.) randomName ((eender: female', region: turkey').then(s>console.log(s)) Ergüç, Semiha randomName ((region: canada', gender: female').then(s>console.log(s)) Gagné, Stella randomName (fregion: 'russia', gender: male').then(sconsole.log(s)) , > randomName ((region: canada', gender: covfefe'J).catch(econsole.log(e.message)) 400 {"error": "Invalid gender"

Explanation / Answer

Hi!

Check this script.

<script>

function randomName(gen,reg) {

var params = {

amount: 1,

gender: gen,

region: reg

};

var queryString = Object.keys(params).map((key) => {

return encodeURIComponent(key) + '=' + encodeURIComponent(params[key])

}).join('&');

var url='https://uinames.com/api/?amount=1'+queryString;

//var url='https://uinames.com/api/?amount=1&gender='+gender+'&region='+region;

return new Promise(

function (resolve, reject) {

const request = new XMLHttpRequest();

request.onload = function () {

if (this.status === 200) {

// Success

resolve(this.response);

} else {

// Something went wrong (404 etc.)

reject(new Error(this.statusText));

}

};

request.onerror = function () {

reject(new Error(

'XMLHttpRequest Error: '+this.statusText));

};

request.open('GET', url);

request.send();

});

}

randomName('female','germany')

.then(

function (value) {

console.log('Contents: ' + value);

},

function (reason) {

console.error('Something went wrong', reason);

});

</script>

Thanks.