Skip to content

Instantly share code, notes, and snippets.

@jdmg94
Created November 15, 2022 17:57
Show Gist options
  • Save jdmg94/8bdd0f2c0fee29639a55999da0aa600b to your computer and use it in GitHub Desktop.
Save jdmg94/8bdd0f2c0fee29639a55999da0aa600b to your computer and use it in GitHub Desktop.
Technical Test from Rainforest
const url = require("url");
// first draft solution
const solveFirstTry = async () => {
const baseURL = "https://www.letsrevolutionizetesting.com/challenge.json";
let id = null;
let isThisTheEnd = false;
do {
let request = baseURL;
if (id) {
request = `${baseURL}?id=${id}`;
}
console.log("hitting: ", request);
const response = await fetch(request);
const result = await response.json();
if (result.follow) {
const { query } = url.parse(result.follow, true);
id = query.id;
} else {
isThisTheEnd = true;
console.log("\n\n");
console.log(result.message);
}
} while (!isThisTheEnd);
};
// in case you wanted recursion solution
const solveOptimized = () => {
const baseURL = "https://www.letsrevolutionizetesting.com/challenge.json";
const solve = async (id) => {
let request = baseURL;
if (id) {
request = `${baseURL}?id=${id}`;
}
console.log("hitting: ", request);
const response = await fetch(request);
const result = await response.json();
if (result.follow) {
const { query } = url.parse(result.follow, true);
return solve(query.id);
} else {
return result.message;
}
};
solve().then((result) => console.log("\n\n%s", result));
};
// they both work the same
Math.random() > 0.5 ? solveFirstTry() : solveOptimized();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment