DEV Community

Discussion on: My family solves a code challenge 🎁

Collapse
 
darrylnoakes profile image
Darryl Noakes • Edited

Could you not just use Array.sort for ordering the dimensions?

Then the relevant piece of code would be:

let [shortest, middle, longest] = [length, width, height].sort((a, b) => a - b);
Enter fullscreen mode Exit fullscreen mode

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 of width longest.
And for that tolerance logic, you can if/else blocks instead of multiple separateif blocks.
Or just use a ternary expression:

let widthTolerance = (middle <= 4) ? width * 0.5 : 2
let lengthTolerance = (longest <= 4) ? length * 0.5 : 2
Enter fullscreen mode Exit fullscreen mode

And since this is just making sure the tolerance never goes above 2, you can just use this:

let widthTolerance = Math.min(middle * 0.5, 2)
let lengthTolerance = Math.min(longest * 0.5, 2)
Enter fullscreen mode Exit fullscreen mode

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:

function wrapThatGift(length, width, height) {
    let arrangedArray = arrangeByValue(length, width, height)

    let longest = arrangedArray[0]
    let middle = arrangedArray[1]
    let shortest = arrangedArray[2]

    let widthTolerance
    if (middle <= 4) {
        widthTolerance = middle * 0.5
    }
    if (middle > 4) {
        widthTolerance = 2
    }

    let lengthTolerance
    if (length <= 4) {
        lengthTolerance = length * 0.5
    }
    if (length > 4) {
        lengthTolerance = 2
    }
    let paperWidth = shortest + 2 * middle + 2 * widthTolerance
    let paperLength = longest + 2 * shortest + 2 * lengthTolerance

    let wrappingPaperSurfaceArea = paperLength * paperWidth

    return `${paperLength} inches by ${paperWidth} inches `
}
Enter fullscreen mode Exit fullscreen mode

To this:

function wrapThatGift(length, width, height) {

  const [shortest, middle, longest] = [length, width, height].sort((a, b) => a - b);

  const widthTolerance = Math.min(middle * 0.5, 2)
  const lengthTolerance = Math.min(longest * 0.5, 2)

  const paperWidth = shortest + 2 * middle + 2 * widthTolerance
  const paperLength = longest + 2 * shortest + 2 * lengthTolerance

  return { 
    length: paperLength,
    width: paperWidth
  }
}
Enter fullscreen mode Exit fullscreen mode

Hope this is useful!

P.S.
Add a language tag to you code blocks to get syntax highlighting.

Collapse
 
cerchie profile image
Lucia Cerchie

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.

Collapse
 
darrylnoakes profile image
Darryl Noakes

Ah, yes, I understand now.
Will update my comment. 👍