Fixed formatting problems, added JSDoc, added example for JS, fixed example for JS

This commit is contained in:
Alexandrshy
2019-07-24 09:25:02 +04:00
parent 84b6d19416
commit a1dbe470cc
4 changed files with 108 additions and 29 deletions

View File

@@ -1,7 +1,3 @@
function person_is_seller(name) {
return name[name.length - 1] === "m";
}
const graph = {};
graph["you"] = ["alice", "bob", "claire"];
graph["bob"] = ["anuj", "peggy"];
@@ -12,6 +8,20 @@ graph["peggy"] = [];
graph["thom"] = [];
graph["jonny"] = [];
/**
* Determine whether a person is a seller
* @param {string} name Friend's name
* @returns {boolean} Result of checking
*/
function personIsSeller(name) {
return name[name.length - 1] === "m";
}
/**
* Find a mango seller
* @param {string} name Friend's name
* @returns {boolean} Search results
*/
function search(name) {
let search_queue = [];
search_queue = search_queue.concat(graph[name]);
@@ -21,7 +31,7 @@ function search(name) {
let person = search_queue.shift();
// Only search this person if you haven't already searched them
if (searched.indexOf(person) === -1) {
if (person_is_seller(person)) {
if (personIsSeller(person)) {
console.log(person + " is a mango seller!");
return true;
}

View File

@@ -0,0 +1,48 @@
const graph = {};
graph["you"] = ["alice", "bob", "claire"];
graph["bob"] = ["anuj", "peggy"];
graph["alice"] = ["peggy"];
graph["claire"] = ["thom", "jonny"];
graph["anuj"] = [];
graph["peggy"] = [];
graph["thom"] = [];
graph["jonny"] = [];
/**
* Determine whether a person is a seller
* @param {string} name Friend's name
* @returns {boolean} Result of checking
*/
function personIsSeller(name) {
return name[name.length - 1] === "m";
}
/**
* Find a mango seller
* @param {string} name Friend's name
* @param {Object} graph Hash table
* @returns {boolean} Search results
*/
function search(name, graph) {
graph = graph || {};
/**
* Recursive function to check people
* @param {Array} waited List of people you need to check
* @param {Array} visited List of checked people
*/
function inner(waited, visited) {
waited = waited || [];
if (waited.length === 0) return false;
const person = waited[0];
const waitedCloned = waited.slice(1);
if (visited.indexOf(person) !== -1) return inner(waitedCloned, visited);
if (personIsSeller(person)) {
console.log(person + " is a mango seller!");
return true;
}
return inner(waitedCloned.concat(graph[person]), visited.concat(person));
}
return inner(graph[name], []);
}
search("you", graph);