Guest User

Untitled

a guest
Nov 14th, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  1. /**
  2. The following code snippet shows how to use named parameters
  3. by using the help of Object Destructuring.
  4.  
  5. Function emiCalculator() takes one argument whcih is a destructured object with
  6. multiple entries, of which two have default values set (in ES6 we can set default values for parameters)
  7.  
  8. When calling the function we would pass three values i.e. rawPrice and downPayment and emiCount (although it has a
  9. default value set) and won't pass any value for 'interestPerAnum'.
  10. For destructured parameters we have pass their values like { parameterName: {properyName: value} }
  11. and access them the similar way, i.e., parameterName.value
  12.  
  13. The function also handes multiple value return, i.e. a JavaScript object
  14. which later we destructure and show the desired output.
  15. */
  16.  
  17. const emiCalculator = ( { rawPrice, downPayment, interestPerAnum = 10, emiCount = 8 } ) => {
  18. const interestApplied = rawPrice.value * ( interestPerAnum / 100 );
  19. const finalPrice = rawPrice.value + interestApplied;
  20. let remainingAmount = ( finalPrice - downPayment.value );
  21. let extendedWarrantyCost = 0;
  22.  
  23. const emiAmount = remainingAmount / emiCount.value;
  24.  
  25. /**
  26. Build multiple values bundled in a JavaScript object.
  27. */
  28. const objEmiDetail = {
  29. rawProductPrice: rawPrice.value,
  30. interestCharged: interestApplied,
  31. finalProductPrice: finalPrice,
  32. downPayment: downPayment.value,
  33. remainingAmount: remainingAmount,
  34. noOfEmi: emiCount.value,
  35. emiAmount: emiAmount
  36. }
  37.  
  38. // return the above object
  39. return objEmiDetail;
  40. }
  41.  
  42. /**
  43. Call function emiCalculator() by immediately destructuring the object objEmiDetail
  44. which is returned by the function.
  45.  
  46. *** Overriding default value of emiCount and skipping default parameter interestPerAnum ***
  47. I have passed three parameters and skipped 'interestPerAnum', which is not the ending parameter.
  48. Meaning, with named parameters, we are not restricted to pass values for all like passing either null or undefined
  49. for paramters sitting in between others with a default value.
  50. Here, the order of the named parameters is rawPrice, downPayment, interestPerAnum, emiCount and we can easily skip the interestPerAnum.
  51. The default value of emiCount (8) will get overridden by the one we pass (12).
  52. */
  53. const {
  54. rawProductPrice,
  55. interestCharged,
  56. finalProductPrice,
  57. downPayment,
  58. remainingAmount,
  59. noOfEmi,
  60. emiAmount} = emiCalculator(
  61. {
  62. rawPrice: { value: 24000 },
  63. downPayment: { value: 8000 },
  64. emiCount: { value: 12 }
  65. }
  66. );
  67.  
  68. /**
  69. Outputting values using template literals
  70. */
  71. console.log(`
  72. Base price: ${rawProductPrice.toFixed(2)}
  73. Interest: ${interestCharged.toFixed(2)}
  74. Purchase Price: ${finalProductPrice.toFixed(2)}
  75. Down Payment: ${downPayment.toFixed(2)}
  76. Remaining: ${remainingAmount.toFixed(2)}
  77. EMI (Remaining / ${noOfEmi}): ${emiAmount.toFixed(2)}`
  78. );
  79.  
  80.  
  81. /**
  82. Output:
  83.  
  84. Base price: 24000.00
  85. Interest: 2400.00
  86. Purchase Price: 26400.00
  87. Down Payment: 8000.00
  88. Remaining: 18400.00
  89. EMI (Remaining / 12): 1533.33
  90. */
Add Comment
Please, Sign In to add comment