Oh, by the way (not being trying to be snarky), you have the wrong variable in the tolerance calculation code. You're using middlelength instead of widthlongest.
And for that tolerance logic, you can if/else blocks instead of multiple separateif blocks.
Or just use a ternary expression:
Also, you never actually use that calculated area.
As a nice-to-have, you can use const instead of let for most of the variables.
If you want, you can return the length and width as an array or object of numbers instead of a string directly, so you could use it for other purposes as well, and just do the string templating when you log it.
So the code can go from this:
functionwrapThatGift(length,width,height){letarrangedArray=arrangeByValue(length,width,height)letlongest=arrangedArray[0]letmiddle=arrangedArray[1]letshortest=arrangedArray[2]letwidthToleranceif(middle<=4){widthTolerance=middle*0.5}if(middle>4){widthTolerance=2}letlengthToleranceif(length<=4){lengthTolerance=length*0.5}if(length>4){lengthTolerance=2}letpaperWidth=shortest+2*middle+2*widthToleranceletpaperLength=longest+2*shortest+2*lengthToleranceletwrappingPaperSurfaceArea=paperLength*paperWidthreturn`${paperLength} inches by ${paperWidth} inches `}
Thanks Darryl! I didn't google anything, so I ended up with longer syntax for some of these -- your solution pares it down nicely! And the code block suggestion is great for blogging on DEV, thanks!
I believe 'middle' is the correct variable though-- and it's actually length that's wrong -- according to the equation we need the middle length for calculating the width tolerance and the longest length for the length tolerance -- so it should be if (longest <= 4) { ...etc.
Could you not just use
Array.sort
for ordering the dimensions?Then the relevant piece of code would be:
Oh, by the way (not being trying to be snarky), you have the wrong variable in the tolerance calculation code. You're using
middle
length
instead ofwidth
longest
.And for that tolerance logic, you can
if/else
blocks instead of multiple separateif
blocks.Or just use a ternary expression:
And since this is just making sure the tolerance never goes above 2, you can just use this:
Also, you never actually use that calculated area.
As a nice-to-have, you can use
const
instead oflet
for most of the variables.If you want, you can return the length and width as an array or object of numbers instead of a string directly, so you could use it for other purposes as well, and just do the string templating when you log it.
So the code can go from this:
To this:
Hope this is useful!
P.S.
Add a language tag to you code blocks to get syntax highlighting.
Thanks Darryl! I didn't google anything, so I ended up with longer syntax for some of these -- your solution pares it down nicely! And the code block suggestion is great for blogging on DEV, thanks!
I believe 'middle' is the correct variable though-- and it's actually length that's wrong -- according to the equation we need the middle length for calculating the width tolerance and the longest length for the length tolerance -- so it should be
if (longest <= 4) { ...etc.
Ah, yes, I understand now.
Will update my comment. π