HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavascriptModerate

Finding the number of boxes that can fit into a truck using JavaScript

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
boxesnumberthecanjavascriptintothattruckusingfinding

Problem


  ////////////////////////////    
 // 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 totalHeight = (truckHeight / boxHeight) | 0;
var totalWidth = (truckWidth / boxWidth) | 0;
var totalLength = (truckLength / boxLength) | 0;

// Calculations
var boxNumber = totalHeight * totalWidth * totalLength;

// Output answer
if (boxNumber = 1)
 {
  alert("You can fit up to" + " " + boxNumber + " " + "box into the truck");
 }
else
 {
  alert("You can fit up to" + " " + boxNumber + " " + "boxes into the truck");  
 }

Solution

You have a bug in the output: your whole program always does the equivalent of

alert("You can fit up to 1 box into the truck");


One way to prevent that kind of error is to use "Yoda conditions", which put the constant first:

if (1 == boxCount)


I think boxCount is a slightly better name than boxNumber, which sounds like it could be referring to the nth box.

Your if-else brace style is probably one of the rarest and least favoured ones possible. The braces, being aligned with neither the outer level nor the inner level, create visual clutter.

I'd actually prefer to use a ternary expression instead of repeating the entire statement:

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


totalHeight, totalWidth, totalLength are misnamed, in my opinion. You're not totalling anything. Furthermore, the values that you store in them are not lengths (with metres as the unit), but unitless numbers. I think heightMultiples, widthMultiples, and lengthMultiples would be more appropriate.

In real life, the problem that you are trying to solve is trickier than that. You have the two options for orienting each box, assuming that the boxes need to stay upright. If you are allowed to tip boxes over on their side, then you have six possible orientations altogether. You would have to try all allowable orientations for optimal packing.

On the other hand, if the boxes are strapped to pallets, you may have no choice at all in the orientation, due to the way the forklift needs to enter the truck.

Fortunately, packing rectangular prisms is a relatively simple problem, compared to other objects. ☺

Code Snippets

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

Context

StackExchange Code Review Q#51327, answer score: 11

Revisions (0)

No revisions yet.