Closed darkterminal closed 1 year ago
If you would like to continue contributing to open source and would like to do it with an awesome inclusive community, you should join our GitHub Organisation - we help and encourage each other to contribute to open source little and often :neckbeard:. Any questions let us know.
This issue will generate as story in our Street Community Programmer site. Thanks for your story!
Metaphore Name
Translate Requests Between Stores with Different Selling Units
Share your metaphore story!
This JavaScript code provides a solution for translating requests between two stores,
storeABC
andstoreDEF
, that sell the same itemABC KOPI MILK
, but with different units of measurement and amounts.The stores are defined as objects with the properties
name
anditems
. Theitems
property is an array of objects, where each object represents a different item and contains the propertiesname
,unit
, andamount
.In this example,
storeABC
has one itemABC KOPI MILK
that is sold in the unit ofJointly
with an amount of 12Jointly
perBox
. On the other hand,storeDEF
has the same itemABC KOPI MILK
that is sold in the unit ofJointly
with anamount
of 1Jointly
perJointly
.The function
convertUnits
takes in two stores,fromStore
andtoStore
, as well as theitemName
of the item to be transferred, and returns the conversion factor necessary to convert the units from one store to the other.Finding the items in both stores
First, the function uses a for...of loop to find the item in both stores, and saves the item objects as the variables
fromItem
andtoItem
. If either of these items is not found in the respective store, the function returns an error message usingconsole.error
.Checking if units are the same
Next, the function checks if the units for the item are the same in both stores. If so, it returns another error message using
console.error
.Calculating the conversion factor
Finally, the function calculates the conversion factor by dividing the amount of
toItem
by the amount offromItem
. The conversion factor is then returned from the function and saved in the variableconversionFactor
. The conversion factor represents the number of units of thetoStore
that can be obtained for each unit of thefromStore
.Logging the conversion factor
Finally, the code logs the conversion factor using
console.log
. The returned value will represent the number of units oftoStore
that can be obtained for each unit offromStore
. In this example, the returned value would be 1 since 1 Jointly is equal to 12 Renteng.Note that the code uses
Math.ceil
to round the conversion factor up to the nearest whole number. This ensures that the returned conversion factor is an integer, even if the calculation results in a fractional value.A demo/repos link
No response