Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.02 KB | None | 0 0
  1. // example of input user
  2. let user1 = {
  3. access: [true, false],
  4. utility: [2, 5]
  5. };
  6. // example of input object
  7. let object1 = {type: 0, cost: 15};
  8.  
  9. // static vertions of the cost function
  10. const costFunction = (user, object) => {
  11. switch (object.type){
  12. case 0:
  13. if(user.access[0]){
  14. return object.cost * user.utility[0];
  15. }else{
  16. return Infinity;
  17. };
  18. case 1:
  19. if(user.access[1]){
  20. return object.cost * user.utility[1];
  21. }else{
  22. return Infinity;
  23. };
  24. };
  25. };
  26.  
  27. costFunction(user1, object1) // sould return 2*15 = 30
  28.  
  29. // dynamically generated a user-adapted const function
  30. const costFunctionGenerator = (user) => {
  31. ... some code ...
  32. const costFunction = (object) => {
  33. ... some code ...
  34. };
  35. return costFunction;
  36. };
  37.  
  38. const user1CostFunction = costFunctionGenerator(user1)
  39.  
  40. const user1CostFunction = (object) => {
  41. switch (object.type){
  42. case 0:
  43. return object.cost * 2;
  44. case 1:
  45. return Infinity;
  46. }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement