Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: The argument of type 'string | undefined' is not assignable to t… #289

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 29 additions & 34 deletions 06_breadth-first_search/ts/01_breadth-first_search.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,37 @@
interface Graph<T> {
[key: string]: T[]
};

function personIsSeller(name: string): boolean {
return name.endsWith('m');
[key: string]: T;
}

const graph: Graph<string> = {};
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 graph: Graph<string[]> = {};
graph["you"] = ["alice", "bob", "claire"];
graph["bob"] = ["anuj", "peggy"];
graph["alice"] = ["peggy"];
graph["claire"] = ["thom", "jonny"];
graph["anuj"] = [];
graph["peggy"] = [];
graph["thom"] = [];
graph["jonny"] = [];

function search(name: string): boolean {
let searchQueue = graph[name];
// This array is how you keep track of which people you've searched before.
const searched = [];
const personIsSeller = (name: string): boolean => name.endsWith("m");

while(searchQueue.length > 0) {
const person = searchQueue.shift();
// Only search this person if you haven't already searched them
if (!searched.includes(person)) {
if (personIsSeller(person)) {
console.log(`${person} is a mango seller!`);
return true;
} else {
searchQueue = searchQueue.concat(graph[person]);
// Marks this person as searched
searched.push(person);
}
}
}
const search = (name: string): boolean => {
let searchQueue: string[] = [...graph[name]];

return false;
const searched: string[] = [];
while (searchQueue.length > 0) {
const person = searchQueue.shift();

if (person && !searched.includes(person)) {
if (personIsSeller(person)) {
console.log(`${person} is a mango seller!`);
return true;
} else {
searchQueue = searchQueue.concat(graph[person]);
searched.push(person);
}
}
}
return false;
};

search('you');
search("you");