# Finding the number of boxes that fit into a truck using JavaScript (Followup Q)

I have made a lot of changes to the code over the course of the evening, but after making most of the suggested changes, the variable that the answer is returning is showing up as "undefined". Could someone please help me find my errors in this code? I have tried and have not been able to find the errors due to my limited knowledge of JS.

<!DOCTYPE html>
<html>
<script>

////////////////////////////
// Code by Zachary Holmes //
////////////////////////////

// This program determines the 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];

// Calculations
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 if (multiplesF > arrayF)
{
boxCount = multiplesF;
}

alert("You can fit up to " + boxCount + (1 == boxCount ? " box" : " boxes") + " into the truck");

</script>
</html>


This is an interesting pattern:

var heightA = boxHeight > 0 ? ((truckHeight / boxHeight) | 0) : 0;


I assume you intend to be using integer division here? Ie, 5/2 | 0 = 2?

Also.. why are you repeatedly declaring different variables with the same values?

var heightA = boxHeight > 0 ? ((truckHeight / boxHeight) | 0) : 0;
var heightB = boxHeight > 0 ? ((truckHeight / boxHeight) | 0) : 0;
var heightC = boxHeight > 0 ? ((truckWidth / boxHeight) | 0) : 0;


Here you are comparing two fundamentally different types:

if (multiplesA > arrayA)
{
boxCount = multiplesA;
}


Comparing different data types in Javascript involves the type coercion system, which in javascript is a Bad Thing -- you're basically putting yourself at the mercy of an extremely unpredictable system. But, beyond that, it's not even clear what your intention with this line is -- how does it make sense to compare a float to an array?

• I am not declaring variables of the same value... – DatLumberZach May 22 '14 at 21:27
• You have repeated variable pairs that have the exact same expression on the right hand side of the equal side: heightA/heightB, heightC/heightD, heightE/heightF, etc. I'm not a JS expert, but I am unaware of any feature in the language that would allow those variables to have different values. – LiavK May 22 '14 at 22:36
• they are all different, look at their values more closely – DatLumberZach May 22 '14 at 22:41
• I also have fixed the code already codereview.stackexchange.com/questions/51454/… – DatLumberZach May 22 '14 at 22:42
• Hey DatLumber, heightA and heightB both have the same expressions: var heightA = boxHeight > 0 ? ((truckHeight / boxHeight) | 0) : 0; var heightB = boxHeight > 0 ? ((truckHeight / boxHeight) | 0) : 0; So do heightC and heightD; heightE and heightF and several other pairs. – LiavK May 22 '14 at 22:48