Fixed formatting problems, added JSDoc, added example for JS, fixed example for JS
This commit is contained in:
@@ -1,18 +1,27 @@
|
||||
const personIsSeller = name => name[name.length - 1] === 'm';
|
||||
|
||||
const graph = {};
|
||||
graph.you = ['alice', 'bob', 'claire'];
|
||||
graph.bob = ['anuj', 'peggy'];
|
||||
graph.alice = ['peggy'];
|
||||
graph.claire = ['thom', 'jonny'];
|
||||
graph.you = ["alice", "bob", "claire"];
|
||||
graph.bob = ["anuj", "peggy"];
|
||||
graph.alice = ["peggy"];
|
||||
graph.claire = ["thom", "jonny"];
|
||||
graph.anuj = [];
|
||||
graph.peggy = [];
|
||||
graph.thom = [];
|
||||
graph.jonny = [];
|
||||
|
||||
const search = (name) => {
|
||||
let searchQueue = [];
|
||||
searchQueue = searchQueue.concat(graph[name]);
|
||||
/**
|
||||
* Determine whether a person is a seller
|
||||
* @param {string} name Friend's name
|
||||
* @returns {boolean} Result of checking
|
||||
*/
|
||||
const personIsSeller = name => name[name.length - 1] === "m";
|
||||
|
||||
/**
|
||||
* Find a mango seller
|
||||
* @param {string} name Friend's name
|
||||
* @returns {boolean} Search results
|
||||
*/
|
||||
const search = name => {
|
||||
let searchQueue = [...graph[name]];
|
||||
// This array is how you keep track of which people you've searched before.
|
||||
const searched = [];
|
||||
while (searchQueue.length) {
|
||||
@@ -31,4 +40,4 @@ const search = (name) => {
|
||||
return false;
|
||||
};
|
||||
|
||||
search('you'); // thom is a mango seller!
|
||||
search("you"); // thom is a mango seller!
|
||||
|
||||
@@ -1,23 +1,35 @@
|
||||
const graph = {};
|
||||
graph.you = ['alice', 'bob', 'claire'];
|
||||
graph.bob = ['anuj', 'peggy'];
|
||||
graph.alice = ['peggy'];
|
||||
graph.claire = ['thom', 'jonny'];
|
||||
graph.you = ["alice", "bob", "claire"];
|
||||
graph.bob = ["anuj", "peggy"];
|
||||
graph.alice = ["peggy"];
|
||||
graph.claire = ["thom", "jonny"];
|
||||
graph.anuj = [];
|
||||
graph.peggy = [];
|
||||
graph.thom = [];
|
||||
|
||||
const isSeller = name => name[name.length - 1] === 'm';
|
||||
/**
|
||||
* Determine whether a person is a seller
|
||||
* @param {string} name Friend's name
|
||||
* @returns {boolean} Result of checking
|
||||
*/
|
||||
const isSeller = name => name[name.length - 1] === "m";
|
||||
|
||||
const search = (name, graph) => {
|
||||
const iter = (waited, visited) => {
|
||||
if (waited.length === 0) {
|
||||
return false;
|
||||
}
|
||||
/**
|
||||
* Find a mango seller
|
||||
* @param {string} name Friend's name
|
||||
* @param {Object} graph Hash table
|
||||
* @returns {boolean} Search results
|
||||
*/
|
||||
const search = (name, graph = {}) => {
|
||||
/**
|
||||
* Recursive function to test people
|
||||
* @param {Array} waited List of people you need to check
|
||||
* @param {Set} visited List of checked people
|
||||
*/
|
||||
const iter = (waited = [], visited) => {
|
||||
if (waited.length === 0) return false;
|
||||
const [current, ...rest] = waited;
|
||||
if (visited.has(current)) {
|
||||
return iter(rest, visited);
|
||||
}
|
||||
if (visited.has(current)) return iter(rest, visited);
|
||||
if (isSeller(current)) {
|
||||
console.log(`${current} is a mango seller!`);
|
||||
return true;
|
||||
@@ -29,4 +41,4 @@ const search = (name, graph) => {
|
||||
return iter(graph[name], new Set());
|
||||
};
|
||||
|
||||
search('you');
|
||||
search("you", graph);
|
||||
|
||||
Reference in New Issue
Block a user