Last active
October 27, 2023 08:35
-
-
Save rix1/601d4fe53943f848fa03d58b69048117 to your computer and use it in GitHub Desktop.
Code understanding. Read through and think out loud: What does this code do? How would you change it? Feel free to use your editor.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const request = ({ method, url, content }) => { | |
if (content && method === 'GET') { | |
throw new Error('GET method with content is not supported'); | |
} | |
if ( | |
!appGlobalsService.get('NODE_ENV') === 'development' && | |
typeof navigator !== 'undefined' && | |
!!navigator && | |
navigator.onLine === false | |
) { | |
log.info('Trying to fetch resource, but no internet connection detected'); | |
return Promise.reject(new Error('offline')); | |
} | |
let requestUrl; | |
const isAbsoluteUrl = /^https?:/.test(url); | |
if (isAbsoluteUrl) { | |
requestUrl = url; | |
} else { | |
requestUrl = `${appGlobalsService.get('OTOVOAPI_URL')}${url}`; | |
} | |
const hasJsonPayload = | |
content && typeof content !== 'string' && !(content instanceof FormData); | |
const headers = { | |
Accept: 'application/json', | |
...(hasJsonPayload ? { 'Content-Type': 'application/json' } : {}) | |
}; | |
let requestData = { | |
headers, | |
method | |
}; | |
if (content) { | |
const payload = hasJsonPayload ? JSON.stringify(content) : content; | |
requestData.body = payload; | |
} | |
try { | |
return doRequest({ | |
requestData, | |
requestUrl | |
}); | |
} catch (error) { | |
log.error(`API: Error requesting ${url}`, error); | |
return { json: null, error }; | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const pairs = [ | |
"A gentleman", | |
"Astronomer", | |
"Conversation", | |
"Dirty room", | |
"Dormitory", | |
"Elegant man", | |
"Funeral", | |
"Listen", | |
"Moon starer", | |
"Real fun", | |
"School master", | |
"Silent", | |
"The classroom", | |
"The eyes", | |
"They see", | |
"Voices rant on", | |
]; | |
/** | |
* An anagram is a play on words created by rearranging the letters of the original word to make a new word or phrase. You can disregard spaces. | |
* | |
* Task: The above list contains 8 pairs of anagrams. The task is to find all pairs. E.g: | |
* | |
* Dormitory <-> Dirty room | |
* Astronomer <-> Moon starer | |
* | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment