Here is the excel formula:
=E2-(J2*2*G2)-(K2*E2*2)/30+IF((L2+M2)>60,((L2+M2)-60)*H2+60*G2,(L2+M2)*G2)+(N2/$AE$1)*$AE$2+(Q2*$AE$5+P2*$AE$4+O2*$AE$3)
Here what I tried (Javascript):
var E2 = $("#sentence").val();
var J2 = $("#deduction").val();
var G2 = 55145;
var K2 = $("#absence").val();
var L2 = $("#overtime").val();
var M2 = 0;
var H2 = 50050;
var N2 = $("#transportation").val();
var sixty;
if ((L2 + M2) > 60) {
sixty = ((L2 + M2) - 60) * H2 + 60 * G2;
} else {
sixty = (L2 + M2) * G2;
};
var result = E2 - (J2 * 2 * G2) - (K2 * E2 * 2) / 30 + sixty;
I couldn't find the way to conver this part of formula:
+(N2/$AE$1)*$AE$2+(Q2*$AE$5+P2*$AE$4+O2*$AE$3)
Here I found the problem:
Even if one of the variables sets to null, then the formula does not work properly.
It looks like some pretty basic math.
let total = (N2 / AE1) * AE2 + (Q2 * AE5 + P2 * AE4 + O2 * AE3 )
This is basically impossible to translate without seeing the actual spreadsheet but that should get you started. Also, make sure to take into consideration order of operations because the computer is going to evaluate it from left to right unless there are parenthesis (where it will evaluate those first).
Related
I am working on mortguage calculator and facing problem when the years value get bigger than it give me infinity in console and nan in result.
// principle / initial amount borrowed
var p = housePriceValue;
// calculate interest rate final value
var interestRateValueByAnum = interestRateValue / 12
var interestRateValuePercentByMonth = interestRateValueByAnum / 100
var i = interestRateValuePercentByMonth.toFixed(4)
var n = numberOfYears * 12
fha.value = p * i * (Math.pow(1 + i, n)) / (Math.pow(1 + i, n) - 1)
I have this formula and it uses powers e.g. '10 to the power 30'
=B2*(B3/12) / (1-(1+(B3/12)) ^ -B6)
The problem is that I can't put ^ in JS/jQuery, I have to use Math.pow() as the equivalent.
I've tried quite a few different positions but I can't seem to get it correct, the brackets really confuse me. Which parts of the formula would I wrap the .pow within?
My attempt: =B2*(B3/12) / Math.pow( (1-(1+(B3/12)) , -B6) )
UPDATE:
Please see this Fiddle
Fiddle code:
var B2 = 500000; //£500,000
var B3 = 2.00; //2.00%
var B6 = 300; //Total number of payments
var original_formula = 'B2*(B3/12)/(1-(1+(B3/12))^-B6)';
var x = B2 * (B3 / 12) / Math.pow(1 - (1 + (B3 / 12)), -B6)
console.log(x);
//Expected/Excel result = 2,119.27
//Actual result = 2.9884337938456618e-229 <- what's going on here?
Have not tried this, but one problem is probably that you're wrapping the two arguments in Math.pow() with a set parentheses.
In your original formula, you wrap the base and exponent in parentheses to group them together:
(1 - (1 + (B3 / 12)) ^ -B6)
Math.pow() takes two arguments, the base, and the exponent:
Math.pow(base, exponent)
You can't use parentheses across two arguments in Javascript, and in fact you don't need to since the pow() function knows that the two arguments should be used together.
So instead of:
Math.pow((1 - (1 + (B3 / 12)), -B6))
I think that should be more like:
Math.pow(1 - (1 + (B3 / 12)), -B6)
=B2*(B3/12) / (1-(1+(B3/12)) ^ -B6)
is the same as
=B2*B3/12 / ( 1 - (1+B3/12) ^ -B6 )
With this reduced number of parantheses it is perhaps easier to see that the javascript implementation should be
= B2*B3/12 / ( 1 - Math.pow(1+B3/12, -B6) )
You also need to translate B3=2.00% into B3=2.00/100 or B3=0.02. Then the result is indeed 2119.2716932202998
var B2 = 500000; //£500,000
var B3 = 2.00; //2.00%
var B6 = 300; //Total number of payments
var original_formula = 'B2*(B3/12)/(1-(1+(B3/12))^-B6)';
B3 = B3/100 // 2.0% is 0.02 as number
var x = B2*B3/12 / (1 - Math.pow(1 + B3/12, -B6) )
var log = document.getElementById("log")
log.innerHTML += "x="+x;
<div id="log"></div>
I don't know how to name a title for my question. Here is the problem:
//Setup all the stats.
var randomStat = Math.floor(Math.random() * ((monster.level + 5) - monster.level + 1) + monster.level);
var multiplier = randomStat * itemQuality.qualityMultiplier;
//Assign the Stats.
var strength = Math.floor(multiplier * itemSubType.strengthMultiplier / 2); //divide each stat by 2 for better balance
var endurance = Math.floor(multiplier * itemSubType.enduranceMultiplier / 2);
var agility = Math.floor(multiplier * itemSubType.agilityMultiplier / 2);
var dexterity = Math.floor(multiplier * itemSubType.dexterityMultiplier / 2);
var wisdom = Math.floor(multiplier * itemSubType.wisdomMultiplier / 2);
var intelligence = Math.floor(multiplier * itemSubType.intelligenceMultiplier / 2);
var luck = Math.floor(multiplier * itemSubType.luckMultiplier / 2);
What it does, is create a randomStat variable and multiplier variable, then use them for each stat. My problem is that if each stat i.e. "strengthMultiplier" is the same as "enduranceMultiplier" which is the case sometimes, then stats will be exactly the same, because randomStat and multiplier is called just once and is used for every stat.
I am trying to create a loop for it, to call it 7 times, and each time its called use it for a single stat, up to 7 stats total.
I could of course create 2 new variables for each stat (total 14) but I hope there is better way to do that, using loops.
I am using javascript, so any help is welcome with javascript only, not jquery or anything else. Thanks :)
You might keep your stats in an array or an object (the latter shown here), and iterate over those:
var stats = {
strenght: null,
endurance: null,
agility: null,
dexterity: null,
wisdom: null,
intelligence: null,
luck: null
};
for (var stat in stats) {
// todo: consider using Object.hasOwnProperty here.
var randomStat = Math.floor(Math.random() * ((monster.level + 5) - monster.level + 1) + monster.level);
var multiplier = randomStat * itemQuality.qualityMultiplier;
stats[stat] = Math.floor(multiplier * itemSubType[stat + 'Multiplier'] / 2);
}
That would store values in a stats dict, i.e. as stats.luck instead of luck.
I'm following a JS tutorial and just made a mortgage calculator that works like a charm in Chrome, but it does nothing in Firefox. Can I get some help figuring this out please? Here's the whole thing:
// Formula: c = ((p * r) * math.pow((1 + r), n)) / (math.pow((1 + r), n) - 1)
//#param p float Amount borrowed
//#param r interest as percentage
//#param n term in years
function percentToDecimal(percent) {
return (percent / 12) / 100;
}
function yearsToMonths(years) {
return years * 12;
}
function calculateMortgage(p, r, n) {
// convert this percentage to decimal:
r = percentToDecimal(r);
n = yearsToMonths(n);
var pmt = ((p * r) * Math.pow((1 + r), n)) / (Math.pow((1 + r), n) - 1);
return parseFloat(pmt.toFixed(2));
}
function postpayments(payment) {
var payments = document.getElementById("outMonthly");
payments.innerText = "$" + payment;
}
var btn = document.getElementById("btnCalculate");
btn.onclick = function () {
var cost = document.getElementById("inCost").value;
var downpayment = document.getElementById("inDown").value;
var interest = document.getElementById("inAPR").value;
var term = document.getElementById("inPeriod").value;
var amountBorrowed = cost - downpayment;
var pmt = calculateMortgage(amountBorrowed, interest, term);
postpayments(pmt);
};
replace payments.innerText with payments.textContent
If you want to work it across all browsers, a clean solution would be to create a TextNode and attach it to the view.
function postpayments(payment) {
var payments = document.getElementById("outMonthly");
while (payments.firstChild!==null)
element.removeChild(payments.firstChild); // remove all existing content
payments.appendChild(document.createTextNode("$" + payment));
}
Check this for more details: https://stackoverflow.com/a/1359834/891092
I am trying to make a simple quadratic formula solver for my own personal use. It works for the most part, but there's one problem: it only works when the answers are Rational numbers (i.e., it won't display sqrt(-1) because that's "i"). When it tries to perform the calculation and the answer isn't a rational, it will display "NaN". My code looks like this:
...*regular html*
<script type = "text/javascript">
var aValue = prompt("What is your 'a' value?");
var bValue = prompt("What's your 'b' value?");
var cValue = prompt("What's your 'c' value?");
var quadFinder = function x_finder(a,b,c) {
document.write((-1 * b + Math.sqrt(b*b - 4*a*c)) / 2*a);
document.write("<br>");
document.write((-1 * b - Math.sqrt(b*b - 4*a*c)) / 2*a);
};
quadFinder(aValue,bValue,cValue)
I know the function is all sound because it will work as long as the answer is only a number.
One other question: what is the Math. command that will round the number? I once put in a few numbers and it came out to some crazy number with around 10 decimal numbers after it.
You might try looking at the discriminant and catching complex cases:
var quadFinder = function x_finder(a,b,c) {
var disc = b * b - 4 * a * c;
if (disc >= 0){
document.write((-1 * b + Math.sqrt(b*b - 4*a*c)) / 2*a);
document.write("<br>");
document.write((-1 * b - Math.sqrt(b*b - 4*a*c)) / 2*a);
} else {
var real = (-1 * b) / (2 * a);
var complex = Math.sqrt(-disc)/(2 * a);
document.write(real + " + " + complex + "i");
document.write("<br>");
document.write(real + " - " + complex + "i");
};