Untitled
unknown
plain_text
a year ago
2.2 kB
7
Indexable
// Stack overflow supporting API methods
// Create methods which support the following operations on stackoverflow website
// == post a question
// == search question using keywords
// == reply to a question
// == Up / Down vote question
/**
* sample entry:
* title: {
* description: "bkla bla",
* replies: [] // array of strings
* }
*
* "First Question": {
* replies: ["ccds"],
* description: "kjhjh"
* }
*/
let userQuestions = {};
let keywordMap = {}; //
function postQuestion({ title, description }) {
if (!title || !description) return;
userQuestions[title] = {
description,
replies: [],
};
title.split(" ").forEach((titleKeyWord) => {
keywordMap[titleKeyWord] = title;
});
return {
message: "Question posted",
data: userQuestions[title],
keywordMap,
};
}
function searchQuestion(keywordSearch) {
if (!keywordSearch)
return {
message: "No search keyword",
};
console.log({ keywordMap });
// assuming the keyword might be title
// if (!keywordMap[keywordSearch]) {
// return {
// message: "No key word found in the title map",
// };
// }
const title = keywordMap[keywordSearch];
console.log(title);
return {
title,
};
}
function replyQuestion({ title, reply }) {
if (!title || !reply)
return {
message: "No title or reply found!",
};
if (!userQuestions[title]) {
return {
message: "Title not found in user questions",
};
}
console.log({ reply, data: userQuestions[title]?.replies });
userQuestions[title]?.replies?.push(reply);
return {
message: "Question posted",
data: userQuestions[title],
};
}
function voteQuestion(question) {}
console.log(
postQuestion({ title: "First question", description: "Testing 1st q" })
);
// ["First", "question"] --> A.contains(searcKeywoird) { push}
// console.log(
// replyQuestion({ title: "First question", reply: "Testing 1st reply" })
// );
// console.log(
// replyQuestion({ title: "First question", reply: "Testing 2nd reply" })
// );
console.log(searchQuestion({ keywordSearch: "question" }));
Editor is loading...
Leave a Comment