My family solves a code challenge 🎁
Tags: javascript, sideprojects, discuss, programming
Published: 2021-12-11 20:11:54

This is a story about what happens when you give a software engineer, a mechanical engineer, and a toddler too much free time...

When I saw @cassidoo 's latest [newsletter code challenge](https://cassidoo.co/newsletter/), it sparked my interest because it reminded me of old SAT problems I used to practice. Here's the problem:

```
You have to order wrapping paper for presents. 

Given the length, width, and height of the boxes 
you need to wrap, 
return the number of square feet (or whatever units you want) 
of wrapping paper you need to order. 

Extra credit: allow for other shapes 
of presents and their dimensions!
```

So, just finding the surface area of an orthotope (gift with rectangular or square sides) could look like this:

```
function getOrthotopeSurfaceArea(length, width, height) {
    let firstVal = length * width * 2
    let secondVal = length * height * 2
    let thirdVal = height * width * 2

    return firstVal + secondVal + thirdVal
}
```
And, as Cassidy suggests, we could make this more difficult by incorporating other shapes. I thought of spheres or [Klein bottles](https://en.wikipedia.org/wiki/Klein_bottle).

But then, I thought about how this would work if I wanted to wrap a present in real life. How would I account for the overlap? The little bit of extra that you fold over the ends? 

This is when I turned to my husband, who has a degree in mechanical engineering, so I guessed he'd be really good at ~~making this challenge harder~~ simplifying the problem. 

He came up with this solution: 

![Image description - lined page with equations and scribbles](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jfjihbmxfq3nk95rl6ns.png)

^ As you can see, our toddler helped him. She informed us that she had written an 'A, a O, a E'. We took note in case this information became useful later. It did not. But we are a team so the goal is respecting each other's input, even if it is not used in the final version of the solution. 

Basically, there are these steps from the notes:

1. Re-order the length, width, and height from longest to shortest. (This would prove to be the hardest step for me to code.)

2. Find the overlap, or length and width tolerances. If the length or width was greater than 4 inches, the tolerance would be 2 inches, otherwise it would be half the length or width.

3. Based on these values, he wrote two equations to solve for the paper's width and length. 
Length = longest entered value + 2(shortest value) + 2(length tolerance)
Width = shortest entered value + 2(middle value) + 2(width tolerance)

Now, the first thing I did was write the code for step 1, how to order three integer values according to magnitude. It was placeholder stuff -- a bunch of 'if... then' conditions that _probably_ covered it, right? But then my husband came back into the room with all the possibilities listed:

![Image description -many boolean expressions written on white paper](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t4m6rlqwpeau8m0plyqn.png)

Holy boolean expressions, batman! I did not want to type all that out. 

So I ended up using a pointer approach for this step. 

```
function arrangeByValue(l, w, h) {
    let arrayToSort = [l, w, h]

    for (let i = 0; i < arrayToSort.length - 1; i++) {
        let firstPointer = arrayToSort[i]
        let secondPointer = arrayToSort[i + 1]

        if (secondPointer > firstPointer) {
            let originalSmallerVal = arrayToSort[i]
            arrayToSort[i] = arrayToSort[i + 1]
            arrayToSort[i + 1] = originalSmallerVal
        }
    }
    return arrayToSort
}
```
Now I could use that function in my code for steps 2 & 3! 

Here's what I did: 

```
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 `
}
```
First, I re-arranged the values using the previous function:

```
    let arrangedArray = arrangeByValue(length, width, height)

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

Then, I found the tolerances:

```
    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
    }
```

Then, I translated the equations into code:

```
    let paperLength = longest + 2 * shortest + 2 * lengthTolerance

    let wrappingPaperSurfaceArea = paperLength * paperWidth
```

And returned the desired values:

```
    return `${paperLength} inches by ${paperWidth} inches `
```

And voila! 

When I ran `node index.js` with a console.logged 
`wrapThatGift(30, 100, 2)`

I got my answer!

`108 inches by 66 inches `

The original repo is here: https://github.com/Cerchie/crinkle-crinkle

We'd love to hear suggestions for improvement or other ways to solve! (If I go back to this, I'm going to create a toleranceFinder func to reduce that logic) Leave me comments to let me know what you think. :)