// PART 1
function PART_1 () {
console.log('PART 1')
console.log(`There is a list of data users in tree diagram structurized and sorted by level called as "Title", but the level was \nunneccesarry because it was pre-defined as a constant data on every users.\nThe task is to make a function to find parent of given element's name that have specific title on it's parent. ( e.g. 'HM', CS1 -> SF -> HM3 )`);
console.log()
var user = (title , id , parentId) => {
return { title , id , parentId, children: [] }
}
var users = []
users.push(user('M','M1','0'))
users.push(user('HM','HM1','M1'))
users.push(user('HM','HM2','HM1'))
users.push(user('HM','HM3','HM1'))
users.push(user('HM','HM4','HM1'))
users.push(user('HM','HM5','HM1'))
users.push(user('SF','SF1','HM3'))
users.push(user('SF','SF2','HM3'))
users.push(user('SF','SF3','HM5'))
users.push(user('SF','SF4','HM5'))
users.push(user('CS','CS1','SF1'))
users.push(user('CS','CS2','SF2'))
users.push(user('CS','CS3','SF3'))
users.push(user('CS','CS4','SF4'))
function list_to_tree(list,keys=['id','parentId','children']) {
var map = {}, node, roots = [], i;
for (i = 0; i < list.length; i += 1) {
map[list[i][keys[0]]] = i; // initialize the map
list[i][keys[2]] = []; // initialize the children
}
for (i = 0; i < list.length; i += 1) {
node = list[i];
if (node[keys[1]] !== "0") {
list[map[node[keys[1]]]][keys[2]].push(node);
} else {
roots.push(node);
}
}
return roots;
}
// console.log(JSON.stringify(list_to_tree(users),null,2))
function searchTree(element, word, key=['id','children']){
if (element[key[0]] == word) {
return element;
} else if (element[key[1]] != null) {
var i;
var result = null;
for(i=0; result == null && i < element[key[1]].length; i++){
result = searchTree(element[key[1]][i], word);
}
return result;
}
return null;
}
var tree = list_to_tree(users);
var search = searchTree(tree[0],'CS4');
const REF_TITLE = 'HM'
// I want to get the LEAF <REF_TITLE> by input the <CHILD_NAME> or id or else
var firstOccurence = true
// I want to get the ROOT <REF_TITLE> by input the <CHILD_NAME> or id or else
// var firstOccurence = false
while (true) {
try {
if (!search) throw new Error('user not found')
else if (search.parentId === '0' && search.title !== REF_TITLE) throw new Error('not found')
if (search.title === REF_TITLE) {
if (firstOccurence) {
console.log(search);
break;
} else {
if (search.parentId === '0') {
console.log(search);
break;
} else {
var tmpSearch = searchTree(tree[0],search.parentId);
if (tmpSearch.title === REF_TITLE) {
search = searchTree(tree[0],search.parentId)
} else {
console.log(search);
break;
}
}
}
} else {
search = searchTree(tree[0],search.parentId)
}
} catch (err) {
console.error(err.message);
break;
}
}
console.log('\n\n')
}
// PART 2
function PART_2 () {
console.log('PART 2')
console.log(`This task is pretty simple, just making a summary of the data and doing a little bit of Math inside.`)
console.log()
var MAX_ORDERS = 100;
var MULTIPLY_BY = .05;
var status = {
'D': 'DRAFT',
'O': 'OK',
'P': 'PENDING',
'C': 'CANCEL',
}
var Item = (itemName, itemPrice) => ({ id: GENID(), itemName, itemPrice });
var Items = [];
var ItemByName = (itemName) => Items.find((d)=>d.itemName.includes(itemName))
const generateRandomPrice = () => (Math.floor(Math.random()*30)*1000+15000)
Items.push(Item('night stand',generateRandomPrice()));
Items.push(Item('window',generateRandomPrice()));
Items.push(Item('sponge',generateRandomPrice()));
Items.push(Item('television',generateRandomPrice()));
Items.push(Item('electrical outlet',generateRandomPrice()));
Items.push(Item('cookie jar',generateRandomPrice()));
Items.push(Item('measuring tape',generateRandomPrice()));
Items.push(Item('cat',generateRandomPrice()));
Items.push(Item('ottoman',generateRandomPrice()));
Items.push(Item('deodorant',generateRandomPrice()));
Items.push(Item('letter opener',generateRandomPrice()));
Items.push(Item('sunglasses',generateRandomPrice()));
var User = (name) => ({ id: GENID(), name })
var Users = [];
var UserByName = (name) => Users.find((d)=>d.name.includes(name))
Users.push(User('John'))
Users.push(User('Maria'))
Users.push(User('Abdoel'))
Users.push(User('Kevin'))
Users.push(User('Samuel'))
Users.push(User('Vhee'))
var Sales = (status, itemName, user) => ({ status, itemName, user });
var SalesOrders = [];
var getRandomStatus = () => {
var keys=Object.keys(status);
var index = Math.floor(Math.random()*keys.length)
return keys[index];
}
var getRandomItem = () => {
var index = Math.floor(Math.random()*Items.length)
return Items[index];
}
var getRandomUser = () => {
var index = Math.floor(Math.random()*Users.length)
return Users[index]
}
for (let i = 0; i < MAX_ORDERS; i++) {
SalesOrders.push(Sales(
getRandomStatus(),
getRandomItem().itemName,
getRandomUser().name,
))
}
let SalesOrdersByUsers = {}
for (let Order of SalesOrders) {
var user = UserByName(Order.user);
var item = ItemByName(Order.itemName);
if (user && item && Order.status === 'O') {
if (!SalesOrdersByUsers[user.id]) SalesOrdersByUsers[user.id] = { name: user.name, totalBonus: 0, items: {} };
if (!SalesOrdersByUsers[user.id].items[item.id]) {
SalesOrdersByUsers[user.id].items[item.id] = {
itemName: item.itemName,
itemPrice: item.itemPrice,
amount: 1,
bonus: item.itemPrice * MULTIPLY_BY,
}
} else {
SalesOrdersByUsers[user.id].items[item.id].amount++;
SalesOrdersByUsers[user.id].items[item.id].bonus+=item.itemPrice * MULTIPLY_BY;
}
SalesOrdersByUsers[user.id].items[item.id].bonusString=IN_CURRENCY(SalesOrdersByUsers[user.id].items[item.id].bonus)
SalesOrdersByUsers[user.id].totalBonus += SalesOrdersByUsers[user.id].items[item.id].bonus;
}
}
function printSummary(name){
var list = Object.values(SalesOrdersByUsers);
if (name) {
console.log('Summary of',name)
list = list.filter((d)=>d.name.includes(name))
}
for (let user of list) {
console.log(`${user.name}, total bonus: ${IN_CURRENCY(user.totalBonus)}`)
for (let item of Object.values(user.items)) {
console.log('-',item.amount, item.itemName, item.bonusString, ` (item price: ${IN_CURRENCY(item.itemPrice)})`)
}
console.log()
}
}
function printSummaryOf(name){
}
printSummary()
// printSummary('Abdoel')
// console.log(SalesOrders.filter(d=>d.status === 'O').length)
console.log('\n\n')
}
PART_1()
PART_2()
// HELPER
var GENID = () => '_'+Math.random().toString(36).substr(2,8); // Generate ID
var IN_CURRENCY = (n) => Intl.NumberFormat('id').format(n); // NUMBER TO CURRENCY FORMAT