0

I am making a simple cost estimator. It takes 3 imputed values X,Y,Z and displays a price which is a simple calculation of a value by the z axis (the third of 3 collected values).

You can view the estimator here: http://codepen.io/FredHair/pen/FgJAd

What I would like to add is a checkbox that when clicked would check that the smallest value is stored as the Z axis. So if the user imputed 3 values and the X or Y value was the smallest then that value would be swapped with with the z axis value.

How would I go about writing a function for this? Any help would be greatly appreciated even in pseudo code, which I could then write myself.

I have made a checkbox:

<input type="checkbox" id="zAxis">Z Axis Optimization<br>

This is my function for the Estimator:

//Calc with Switch//
function calculator(){
    var x = Number(document.getElementById("x").value);
    var y = Number(document.getElementById("y").value);
    var z = Number(document.getElementById("z").value);
    var p = Number(3);
    var result;
  var calc = document.getElementById("choice").value
    switch(calc){
        case"1" : result = z * p; 
    break;
        case"2" : result = (z * p) + 50; 
    break;
        case"3" : result = (z * p) + 30; 
    break;
  }
 //Display Result//
 document.getElementById("result").innerHTML = " =  £  " + result;

I know I will need to write a function like:

IF (x<=y && <=z) then do something
OR (y<=y && <=x) then do something

But any help will be greatly appreciated. Here is Codepen again: http://codepen.io/FredHair/pen/FgJAd

Thanks in advance.

Mr Boom
  • 41
  • 3
  • 10
  • You could just do something like if (x < z) { // swap x and z } and then if (y < z) { // swap y and z }. That way you are guaranteed that z will be the minimum value. Swapping is easily done by using another variable, like this: var a = x; x = y; y = a; Do you also want the code for the actual swapping between the HTML inputs? – Tiborg Oct 13 '14 at 15:05
  • Yes that would be a great help. – Mr Boom Oct 13 '14 at 15:07
  • Just one more comment, in my example, if you have let's say x = 4, y = 2 and z = 6, then you would get x = 6, y = 4 and z = 2 (so x was actually moved to y, although it wasn't necessary). Is there a problem with that? Or you want to swap only the minimum value? – Tiborg Oct 13 '14 at 15:11
  • Only the minimum value is needed to be swapped with the Z value so the other value could remain the same. So for example with: X=10 Y=4 Z=8 you would get X=10 Y=8 Z=4. With: X=6 Y=7 Z=10 you would get X=10 Y=7 Z=6. and with: X=5 Y=6 Z=2 nothing would change. – Mr Boom Oct 13 '14 at 15:23

0 Answers0