Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function dictionary(arr) {
- let definitions = arr[0].split(" | ");
- let definitionsToPrint = arr[1].split(" | ").sort((a, b) => a.localeCompare(b));
- let lastCommand = arr[2];
- let dictionary = {};
- definitions.forEach(element => {
- dictionary = dictionaryPopulation(dictionary, element);
- for (let word in dictionary) {
- dictionary[word].sort((a, b) => b.length - a.length || a.localeCompare(b));
- };
- });
- definitionsToPrint.forEach(definition => {
- if (dictionary[definition] !== undefined) {
- console.log(`${definition}\n -${dictionary[definition].join(`\n -`)}`);
- }
- })
- if (lastCommand === "End") return;
- if (lastCommand === "List") console.log(Object.keys(dictionary).sort((a, b) => a.localeCompare(b)).join(" "));
- function dictionaryPopulation(obj, input) {
- let [word, definition] = input.split(": ");
- if (obj[word] === undefined) {
- obj[word] = [];
- }
- obj[word].push(definition);
- return obj;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement