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

Lection 1 homework #1

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
# JS-Proffessional-03-2023
OTUS JavaScript Proffessional course repo
9 changes: 9 additions & 0 deletions src/carrying.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
let sum = (arg) => {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const, можно работать с spred оператором

return (arg === undefined) ? 0 : function inner(innerArg) {
arg += innerArg | 0;
return (innerArg === undefined) ? arg : inner;
};
}

console.log(sum(12)(13)(14)(0)(1)()); //40
console.log(sum()); //0
51 changes: 51 additions & 0 deletions src/list.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//it just fast-borned simple algorithm from the head, I think it does not too fast and memory optimized

//uncomment items for larger result list
//"r" extends list for "v"
//"d" extends list for "d, x, y, t, l, m, z"
//"d + l" extends for "d, x, y, t, l, m, z, f, g"
//uncommenting only "l" extends for none
let prodsList = [["q", "w", "a"], ["a", "b"/*, "d"*/], ["a","c"], ["q", "e"], ["q", "r"], ["x", "y", "z"], ["x", "d"], ["z", "m"], ["f", "g"/*, "l"*/], ["t", "l", "y"], [/*"r", */"v"]];

//if result is an object, algorithm must to be too match faster, because "indexOf" did not used
let result = [];

let uniProds = {};
let mostPopular = {name : "",
popularity : 0}

prodsList.forEach((list) => {
if (list.length < 2) {
return;
}

for (let name of list) {
uniProds[name] ??= { childs : {}, name : name, popularity : 0 };
let prod = uniProds[name];
(prod.popularity ++ >= mostPopular.popularity) && (mostPopular = prod);
//element itself do add to childs tree alwais, its necessary (!!)
list.forEach((el) => {
uniProds[el] ??= { childs : {}, name : el, popularity : 0 };
prod.childs[el] ??= uniProds[el];
prod = uniProds[el];
});
}
});

(getResult = (prod) => {
if (prod.childs) {
//if recursion does find existing item, simple waste this tree branch
for (let name in prod.childs)
//if it does find non-existing item, adding it to the result list
if (result.indexOf(prod.childs[name].name) < 0) {
result.push(prod.childs[name].name);
getResult(prod.childs[name]);
}
}
return result;
})(mostPopular).sort((a, b) => {
return a > b; //sort result list by name
// return uniProds[a].popularity < uniProds[b].popularity; //sort result list by popularity
});

console.log(result);