Guest User

Untitled

a guest
May 27th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.08 KB | None | 0 0
  1. var inventory = {
  2. '000001': {
  3. name: 'Banana Slicer',
  4. price: 2.99
  5. },
  6. '000002': {
  7. name: 'Three Wolves Tea Cozy',
  8. price: 14.95
  9. }
  10. };
  11.  
  12. function tenPercentOffOf (obj) {
  13. obj.price = Math.round(0.9 * obj.price * 100) / 100;
  14. return obj;
  15. }
  16.  
  17. // INSTRUCTIONS
  18. //
  19. // Please refer to the following Javascript variable declaration. When submitting your
  20. // application, please answer the following three questions at the top of your cover
  21. // letter, before any salutations or headings.
  22. //
  23. // var bargainBananaSlicer = tenPercentOffOf(inventory['000001']);
  24. //
  25. // Question 1: After declaring that variable, what is the value of bargainBananaSlicer.price?
  26. // Question 2: After declaring that variable, what is the price of the Banana Slicer
  27. // in the inventory object?
  28. // Question 3: Can you suggest ways to improve this code?
  29.  
  30.  
  31.  
  32. //ANSWERS
  33. // Question 1: After declaring that variable, what is the value of bargainBananaSlicer.price
  34. // Answer 1: 'bargainBananaSlicer.price` would be 2.69
  35.  
  36. // Question 2: After declaring that variable, what is the price of the Banana Slicer in the inventory object?
  37. // Answer 2: Value of banana Slice would be 2.69 Because object passed in the function was not clone or cached into variable
  38. //so when mutation process it affects the orignal value.
  39.  
  40. // Answer 3.
  41. // Object.assign create a swallow copy of the object.
  42. /**
  43. * @param {} obj Object
  44. * Return Ten percent of price off
  45. */
  46. const tenPercentOffOf = (obj) => {
  47. if (!obj.hasOwnProperty("price")) return console.warn("Property price does not exist");
  48. return Object.assign({}, obj, {
  49. price: Math.round(0.9 * obj.price * 100) / 100
  50. });
  51. }
  52.  
  53. // Sample 2. using lodash _.clone
  54. // _.clone create a swallow copy of __obj
  55. /**
  56. * @param {} obj Object
  57. * Return Ten percent of price off
  58. */
  59. const tenPercentOffOf = (obj) => {
  60. let obj = _.clone(__obj);
  61. if (obj.hasOwnProperty("price")) {
  62. obj.price = Math.round(0.9 * obj.price * 100) / 100;
  63. return obj;
  64. } else {
  65. console.warn("Property price does not exist")
  66. }
  67. }
Add Comment
Please, Sign In to add comment