0

Does anyone know why the exponent and round up functions aren't giving me the correct answers? I've gone through a load of debugging and I can't figure out what I'm doing that's making it's value abnormally large (like 25 orders larger)

function CAPEX(){
var initial = document.getElementById("CAPEX").value;
var lifespan = document.getElementById("life").value;
var interest = document.getElementById("IR").value;
var capitalrepayment = initial/lifespan;
var i;
var cap=0;
var expense =0;
for (i=0; i<lifespan;i++){
    expense = capitalrepayment + (initial*interest);
    cap = cap + expense;
    initial = initial - capitalrepayment;
}
denominator = Math.pow((1+interest), lifespan);
console.log(denominator);
return cap;

}

I have a similar sort of issue here too where Math.ceil is returning a completely different answer too.

 if(selected_scenario == "Divers"){
   var totalTechnicians = numberNeeded * 3;
   var supervisors = totalTechnicians/3;
   var othertechnicians = totalTechnicians-supervisors;
   var boats= Math.ceil((totalTechnicians+numberNeeded)/12);
   divertotal = (numberNeeded*580)+(supervisors*516)+(othertechnicians*276)+(boats*2345)+9230+(boats*20);
}

For reference interest is 0.02, lifespan is 25, numberNeeded is 23. I'm reading these values in directly from a form number input.

The denominator should be 1.64 and boats should be 8.

  • 1
    This: `Math.pow( 1 + "0.02", "25" )` is converting the number `1` to the string `"1"` and concatenating `"0.02"` to give `"10.02"`, then converting it to a number (`10.02`) and raising that to the 25th power. – Tibrogargan Mar 05 '21 at 18:54

1 Answers1

2

The values that you are getting from the DOM are strings. You need to convert them to numbers before you can apply any mathematical operations to them. Try using parseInt() or parseFloat()

Sultan Singh Atwal
  • 810
  • 2
  • 8
  • 19