Trying to get max value of two variables..works in one place, not in the other, not sure why. Help anyone? (please & thank you!)

OK, I’ll post the code in it’s entirely (html/touch of CSS/& JS) so you can see where I’m getting things from…

It’s a calculator for shipping purposes, and 95% of it is functional. If you select “CM,” any and all calculations there are correct. If you select “IN,” the calculations are correct, however it does NOT compare the value of “volumelbs” to that of “actualweight” to determine which is larger.

That is the issue I’m running into. (hope this makes sense…new to code forums, so my wording may not be on par)

Here’s the code (let me know if I should edit it out and add a fiddle or js.do instead).

<script src= 
"https://code.jquery.com/jquery-1.12.4.min.js"> 
    </script> 
    <style type="text/css"> 
        .selectt { 
            display: none;
      }
      #et-boc > div > div > div > div > div > div.et_pb_module.et_pb_code.et_pb_code_1 > div > div.Inches.selectt { display: block; }
</style>


<div style="text-align:center;"> 
	<div style="width:49%; float:left;">
    <label> 
         <input type="radio" name="colorRadio" 
          value="Inches" checked> Inches</label> 
  	</div>
	<div style="width:49%; float:left;">
		<label> 
  		<input type="radio" name="colorRadio" 
          value="Centimeters"> Centimeters</label> 
  </div>
</div>
<br>
<div style="width:45%;float:left; margin-right:10%;">
  <p>Length</p>
  <p>Width</p>
  <p>Height</p>
  <br>
  <p>Actual Weight</p>
  <p>Volumetric Weight</p>
  <br>
  <p>Total Cost*</p>
</div>
<div class="Centimeters selectt" style="width:45%; float:left; text-align:right;"> 
<div>
  <p><input type="Text" id="length" name="length" size="4" onkeyup=""> cm</p>
  <p><input type="Text" name="width" id="width" size="4"> cm</p>
  <p><input type="Text" id="height" name="height" size="4" onkeyup=""> cm</p>
  <br>
  <p><input type="Text" name="actualWeight" id="actualWeight" size="4"> kg<span style="opacity:0"> </span> </p>
  <p><input type="Text" name="volume" id="volume" size="4" readonly="readonly" style="border:none;"><span style="opacity:0">s</span> <span style="opacity:0">s</span> kg<span style="opacity:0"> </span> </p>
  <br>
  <p>$<input type="Text" name="totalcost" id="totalcost" size="4" readonly="readonly" style="border:none;">   </p>
</div>
<br>
<p><input type="button" name="calculate" id="volume" size="4" onclick="calculateTotal()" value="Calculate"></p>
</div>
<div class="Inches selectt" style="width:45%; float:left; text-align:right;"> 
  <div>
  <p><input type="Text" id="lengthlbs" name="lengthlbs" size="4" onkeyup=""> in<span style="opacity:0">s</span> </p>
    <p><input type="Text" name="widthlbs" id="widthlbs" size="4"> in<span style="opacity:0">s</span> </p>
  <p><input type="Text" id="heightlbs" name="heightlbs" size="4" onkeyup=""> in<span style="opacity:0">s</span> </p>
  <br>
  <p><input type="Text" name="actualWeight" id="actualWeight" size="4"> lbs</p>
  <p><input name="volumelbs" id="volumelbs" size="4" readonly="readonly" style="border:none;"><span style="opacity:0">s</span> <span style="opacity:0">s</span>  lbs</p>
  <br>
  <p>$<input type="Text" name="totalcost2" id="totalcost2" size="4" readonly="readonly" style="border:none;">   </p>
</div>
<br>
<p><input type="button" name="calculate" id="volumelbs" size="4" onclick="calculateTotal()" value="Calculate"></p>
</div>


<script>
$(document).ready(function() { 
	$('input[type="radio"]').click(function() { 
		var inputValue = $(this).attr("value"); 
		var targetBox = $("." + inputValue); 
		$(".selectt").not(targetBox).hide();
		$(targetBox).show(); 
	}); 
}); 

function calculateTotal (){
	var width = +document.getElementById("width").value;
	var height =+document.getElementById("height").value;
 	var length =+document.getElementById("length").value;
  	var actualWeight =+document.getElementById("actualWeight").value;
  	var actualWeight = actualWeight.toFixed(1);
	var actualWeight = Math.ceil(actualWeight*2)/2;
  	var volume = (width * height * length) / 5000;
	var volume = volume.toFixed(1);
	var volume = Math.ceil(volume*2)/2;
  	document.getElementById("volume").value = volume;
  	let billedWeight = Math.max(volume, actualWeight);
  	var widthlbs = +document.getElementById("widthlbs").value;
	var heightlbs =+document.getElementById("heightlbs").value;
	var lengthlbs =+document.getElementById("lengthlbs").value;
	var volumelbs = (widthlbs * heightlbs * lengthlbs) / 138.4;
	var volumelbs = volumelbs.toFixed(1);
	var volumelbs = Math.ceil(volumelbs*2)/2;
	document.getElementById("volumelbs").value = volumelbs; 
  	let billedWeight2 = Math.max(volumelbs, actualWeight);
	if (volume > 0){
    	var totalcost = ((billedWeight*2.2046226218)/2)*5;
      var totalcost = totalcost.toFixed(2);
		  document.getElementById("totalcost").value = totalcost;
    }
    if (volumelbs > 0) {
    	var totalcost2 = (billedWeight2*2)*5;
    	document.getElementById("totalcost2").value = totalcost2;
    }
    
} 
</script>

Welcome to the Free Code Camp forum.

Can you walk us through a couple of test cases (what values to enter into the input fields) and what you expected to see when the button is clicked vs. what you are seeing? That would help us understand the overall intent of the code better.

Well you can select “in” and then enter in anything. OR enter in nothing except for “actual weight” and hit calculate. It SHOULD run calculations off the actual weight alone (since it’s registering it as larger than then volumelbs, which is 0 without the length/width/height.

For example
select “in”
length = 5
width = 5
height = 5
actual weight = 5
Calculate --> volumetric weight = 1
It should now take the larger of those two variables (actual & volumetric), divide by 2, and multiply by 5 (or just multiply the larger variables by 10.)
SOMEHOW it’s not registering the larger of those two. (but it does for CM)

However, if you run it with just the actual weight, you get “0” as a total cost.

That’s the quickest way to see the issue.

Select CM, do the same, you’ll see calculations. Or enter info, calculations.

Only running into issues with getting the largest of the two variables “volumelbs” (registers/calculates fine) and “actualWeight”

The same variable “actualWeight” is used prior to the comparison with “volumelbs” (compared against variable “volume”) and it works fine.

I’ve even tried “var actualWeight2 = actualWeight;” (for a fresh, “unused” variable) and then swapping things out to show “Let billedWeight2 = Math.max(volumelbs, actualWeight);” (and then calculations below as well), but it has no effect. Same results.