7
\$\begingroup\$

This code determines the maximum number of boxes (one size) that can fit into the back of a truck, checking all possible orientations to ensure maximum Bin Packing excitement. This is (hopefully) the final version of this code. Please share any modifications that you would make to this code below.

<!DOCTYPE html>
<html>
<script>
// This program determines the maximum number of boxes that can fit into the back of a truck
// Declaring box size in centimetres, and converting it into metres
var boxHeight = prompt("What is the height of your box in centimetres?") / 100;
var boxWidth = prompt("What is the width of your box in centimetres?") / 100;
var boxLength = prompt("What is the length of your box in centimetres?") / 100;
// Declaring truck size in metres
var truckHeight = prompt("What is the height of your truck in metres?");
var truckWidth = prompt("What is the width of your truck in metres?");
var truckLength = prompt("What is the length of your truck in metres?");
// Declaring variables used in equation
var heightA = boxHeight > 0 ? ((truckHeight / boxHeight) | 0) : 0;
var widthA = boxWidth > 0 ? ((truckWidth / boxWidth) | 0) : 0;
var lengthA = boxLength > 0 ? ((truckLength / boxLength) | 0) : 0;
var heightB = boxHeight > 0 ? ((truckHeight / boxHeight) | 0) : 0;
var widthB = boxWidth > 0 ? ((truckLength / boxWidth) | 0) : 0;
var lengthB = boxLength > 0 ? ((truckWidth / boxLength) | 0) : 0;
var heightC = boxHeight > 0 ? ((truckWidth / boxHeight) | 0) : 0;
var widthC = boxWidth > 0 ? ((truckHeight / boxWidth) | 0) : 0;
var lengthC = boxLength > 0 ? ((truckLength / boxLength) | 0) : 0;
var heightD = boxHeight > 0 ? ((truckWidth / boxHeight) | 0) : 0;
var widthD = boxWidth > 0 ? ((truckLength / boxWidth) | 0) : 0;
var lengthD = boxLength > 0 ? ((truckHeight / boxLength) | 0) : 0;
var heightE = boxHeight > 0 ? ((truckLength / boxHeight) | 0) : 0;
var widthE = boxWidth > 0 ? ((truckWidth / boxWidth) | 0) : 0;
var lengthE = boxLength > 0 ? ((truckHeight / boxLength) | 0) : 0;
var heightF = boxHeight > 0 ? ((truckLength / boxHeight) | 0) : 0;
var widthF = boxWidth > 0 ? ((truckHeight / boxWidth) | 0) : 0;
var lengthF = boxLength > 0 ? ((truckWidth / boxLength) | 0) : 0;
var multiplesA = heightA * widthA * lengthA;
var multiplesB = heightB * widthB * lengthB;
var multiplesC = heightC * widthC * lengthC;
var multiplesD = heightD * widthD * lengthD;
var multiplesE = heightE * widthE * lengthE;
var multiplesF = heightF * widthF * lengthF;
var arrayA = [multiplesB, multiplesC, multiplesD, multiplesE, multiplesF]; 
var arrayB = [multiplesA, multiplesC, multiplesD, multiplesE, multiplesF];
var arrayC = [multiplesB, multiplesA, multiplesD, multiplesE, multiplesF];
var arrayD = [multiplesB, multiplesC, multiplesA, multiplesE, multiplesF];
var arrayE = [multiplesB, multiplesC, multiplesD, multiplesA, multiplesF];
var arrayF = [multiplesB, multiplesC, multiplesD, multiplesE, multiplesA];
// Calculating the most efficient orientation of the boxes
var boxCount;
if (multiplesA > arrayA)
{
boxCount = multiplesA;
}
else if (multiplesB > arrayB)
{
boxCount = multiplesB;
}
else if (multiplesC > arrayC)
{
boxCount = multiplesC;
}
else if (multiplesD > arrayD)
{
boxCount = multiplesD;
}
else if (multiplesE > arrayE)
{
boxCount = multiplesE;
}
else
{
boxCount = multiplesF;
}
// Output answer
alert("You can fit up to " + boxCount + (1 == boxCount ? " box" : " boxes") + " into the truck");
</script>
</html>
200_success
145k22 gold badges190 silver badges478 bronze badges
asked May 22, 2014 at 21:53
\$\endgroup\$

2 Answers 2

4
\$\begingroup\$

I think a big first improvement for you will be to start DRYing out your code. You should create a function which returns the ratio of two sides -- there is much too much repetition in your code. Something like:

function get_ratio(x, y) {
 if (y === 0) {
 return 0
 }
 return x/y | 0
}
answered May 22, 2014 at 22:54
\$\endgroup\$
1
  • \$\begingroup\$ Yes, I am new to JS so my code uses what I know already \$\endgroup\$ Commented May 22, 2014 at 22:55
1
\$\begingroup\$

Your program is O(D!) in complexity, where D is the number of dimensions (i.e., 3). What would happen if you wanted to write a program to pack four-dimensional boxes into a four-dimensional truck? Your program just doesn't scale.

Just kidding! In seriousness, though, while O(D!) running time may be unavoidable, your program could still be structured to not have copy-and-paste code.

Start with

var axes = ['height', 'width', 'length'];

and see what you can do. (I've chosen the word axes in preference to dimensions, since the latter could also mean a measurement along an axis.)

answered May 23, 2014 at 23:49
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.