jQuery function calculation wrong - javascript

I have this function
$("#exchange").on("change", function() {
var am = $(this).val();
var fee = $("#fee").val();
var cost = $("#cost").val();
var perc = fee / 100;
var tot = perc * am;
var fea = parseFloat(tot) + parseFloat(cost);
var total = parseFloat(am) - parseFloat(tot) - parseFloat(cost);
$("#return").val(total.toFixed(2));
$("#due").val("$" + fea.toFixed(2));
});
$("#return").on("change", function() {
var am = $(this).val();
var fee = $("#fee").val();
var cost = $("#cost").val();
var perc = fee / 100;
var tot = perc * am;
var fea = parseFloat(tot) + parseFloat(cost);
var total = parseFloat(am) + parseFloat(tot) + parseFloat(cost);
$("#exchange").val(total.toFixed(2));
$("#due").val("$" + fea.toFixed(2));
});
for example if #exchange = 16.85, #fee = 11, and #cost = 0
it should calculate #due = $1.85 and #return = 15.00
which is all correct. The problem is working in reverse I need it to calculate the same way but instead right now I get this
#return = 15, #fee = 11, and #cost = 0
it calculates #due = $1.65 and #exchange = 16.65
I understand why it is doing that, because it is calculating the fees from the var am which is the value of that field, which makes it very difficult for me to accomplish what I am trying to achieve which is to make it the same both ways, but obviously I cannot call var am to be the value of #exchange in my #return function because the field would be blank at that time which would calculate it at NAN so what can I do to make it to where all of my calculations are the same both ways, I have been trying for the better part of 5 hours to figure out what to do, but I am lost, a point in the right direction would be greatly appreciated.

This looks like a simple problem with your algebra to me. I see what you are trying to do. As it stands, you assume return = exchange - tot - cost and exchange = return + tot + cost. The problem is that you have assumed var tot is the same in your solution working forward and your solution working in reverse.
However, tot is equal to (fee/100) * exchange working forward and (fee/100) * return working backward, which breaks your assumptions for how to calculate exchange and return. My first step would be to move away from the var am assignment and naming, which seems to be confusing you, and call each exchange and return what they really are. This might help you form a correct algebraic solution, which you can then implement in JavaScript.
JSFiddle: https://jsfiddle.net/z6hrLbmc/

You are using wrong formula.
Use brutto=netto/(1-fee/100) instead of brutto=netto*(1+fee/100)
You have to distinguish whether the fee is applied to netto (+netto*fee/100) or to brutto (-brutto*fee/100).

Related

GlideRecord ServiceNow Retrieving Int from Field for every record

I am new to Service Now and have tried many times to get the data I am looking for. I have a script that grabs all Virtual Machines in our database and takes the CPU count and the core count. I am looking to calculate the kWh usage for all our VMs. The formula is correct and I can run it just fine offline, but when it comes to Gliderecords, I think I am not grabbing the right data. any help would be greatly appreciated. Thank you
var calckWh = function() {
var gr = new GlideRecord('cmdb_ci_server');
var final_result = 0;
var numOfCores;
var numOfCPUs;
gr.addQuery('Model', 'Microsoft Corporation Virtual Machine');
gr.addQuery('CPU core count', '>=', 1);
var qc = gr.addQuery('Install Status', 'Installed');
qc.addOrCondition('Install Status', 'In Use');
gr.query();
while (gr.next()) {
numOfCores = gr.getValue('cpu_core_count');
numOfCPUs = gr.getValue('cpu_count');
for (var j = 0; j < numOfCPUs; j++) {
var TDP = 165;
var load0 = 30;
var load1 = 50;
var load2 = 80;
var load3 = 90;
var load4 = 50;
var load5 = 30;
var hours = 4;
var result = 0;
var i;
for (i = 0; i <= 5; i++) {
var currentLoad = "load" + i;
result += (numOfCores * TDP * (eval(currentLoad) / 100) * hours) / 1000;
}
final_result += result;
}
}
return final_result;
This script grabs all installed/in-use VMs, and while there is another one in the records, it will run through, grab the current CPU and core count of the VM, then it will do the calculation of the number of times there are CPUs in the VM. it adds all of these calculations up to get the total kWh used for all of our active VMs. The only problem is I should be getting a number between 15,000-16,000, but this results in 229,000. I am not sure why.
You cannot use field labels or display values in an addQuery. You must use field names (which are always lower case) and field values. Your first addQuery should look something like this ...
gr.addQuery('model_id', '15e8f8d537a01000deeabfc8bcbe5d46');
... although it is just an example. I do not know the sys_id of "Microsoft Corporation Virtual Machine" in your instance.
If you do not want to hard-code the sys_id of the model, you can dot-walk to the model table like this...
gr.addQuery('model_id.display_name', 'Microsoft Corporation Virtual Machine');
"Installed" and "In Use" are two different names for the same thing, so you probably do not need an "OR" condition. Just use...
gr.addQuery('install_status', '1');
If you need to select multiple values, it is easier to use an "IN" operator
gr.addQuery('install_status', 'IN', '1,4');
I suspect that all your addQuery statements are being ignored, and that you are doing some sort of calculation on all the servers in your instance.
The getValue method always returns a string, regardless of the underlying data type. Instead of ...
numOfCores = gr.getValue('cpu_core_count');
numOfCPUs = gr.getValue('cpu_count');
it is better to write ...
numOfCores = parseInt(gr.getValue('cpu_core_count'));
numOfCPUs = parseInt(gr.getValue('cpu_count'));
Another problem may be eval(currentLoad). You are not supposed to use eval in a ServiceNow script. You are supposed to use GlideEvaluator or GlideScopedEvaluator. In this case you do not even need to call eval. Just use an array. It is simpler and more efficient.
var load = [30, 50, 80, 90, 50, 30];
then
result += (numOfCores * TDP * (load[i]) / 100) * hours) / 1000;

I'm posting a tiny part of my code that is giving me an error. Only two lines need to be fixed to avoid "not a number' error

The following sections on my code are returning NaN error:
var nextTrain = frequency - restAferLastTrain;
var nextArrival = moment().add(nextTrain,
I was thinking that I may need to use moment.js to compute the calculations because they are dealing with time. If so, how can I fix them?
database.ref().on("child_added", function(childSnapshot, prevChildKey) {
console.log(childSnapshot.val());
//store in variables
var trainName = childSnapshot.val().train;
var destination =childSnapshot.val().trainOut;
var firstTime = childSnapshot.val().trainIn;
var frequency = childSnapshot.val().interval;
//makes first train time neater
var trainTime = moment.unix(firstTime).format("hh:mm");
//calculate difference between times
var difference = moment().diff(moment(trainTime),"minutes");
//time apart(remainder)
var restAferLastTrain = difference % frequency;
//minutes until arrival
var nextTrain = frequency - restAferLastTrain;
//next arrival time
var nextArrival = moment().add(nextTrain, "minutes").format('hh:mm');

Trying to learn javascript. Working on retirement calculator. Need Loop advice

**Update I have chance previous variable to return a value. I still have no solution for the loop error. When I submit it returns $5.1572...., when it should be returning around 200k. **
I have a retirement calculator that I have created for some online classes and I cannot get the loop to work. I assume that's what it is.
I have verified that the calculator is working and have no other errors but just don't seem to understand how to use the loop properly.
Any help would be appreciated.
Trying to take retire (age) - current (age) to come up with lengthOfCalulation. From there I need to take the Return (% of interest) and the PerYear ($ of investment each year) and determine the future value.
function Savings() {
var Current = Number(document.getElementById('AgeNow').value);
var Retire = Number(document.getElementById('AgeThen').value);
var Return = Number(document.getElementById('Return').value);
var PerYear = Number(document.getElementById('PerYear').value);
var lengthOfCalculation = Retire - Current;
var results;
var total = 0;
for (results=0; results < lengthOfCalculation; results++) {
total = (total + PerYear) * (1 + Return);
}
alert("When you retire your account will have $" + total.toFixed(2));
}
You are creating the variable total in each iteration and you are not using your results variable in your calculation in the loop. This loop just calculates the same result each time so it would be the same without looping at all.
Try this
function Savings() {
var Current = Number(document.myForm.AgeNow.value);
var Retire = Number(document.myForm.AgeThen.value);
var Return = Number(document.myForm.Return.value);
var PerYear = Number(document.myForm.PerYear.value);
var lengthOfCalculation = Retire - Current;
var results;
var total = 0;
for (results=0; results < lengthOfCalculation; results++) {
total += (PerYear * (1 + Return));
}
alert("When you retire your account will have $" + total.toFixed(2));
}

Average of four fields in a form

form.getElement('ar_essay1_read1_subscore4').bind('change', function() {
calcEssayScore();
}).bind('keyup', function() { $(this).triggerHandler('change'); });
function calcEssayScore()
{
var score1 = form.getElement('ar_essay1_read1_subscore1').val();
var score2 = form.getElement('ar_essay1_read1_subscore2').val();
var score3 = form.getElement('ar_essay1_read1_subscore3').val();
var score4 = form.getElement('ar_essay1_read1_subscore4').val();
var recalc = ((score1 + score2 + score3 + score4) / 4);
form.getElement('sys:app:ar_essay1_read1').val(recalc.toFixed(3));
}
I know this is a total rookie javascript error, but this keeps returning errors. If i put 4 into each field in the form, it's return 1111.000 as the sys:app:ar_essay1_read1 score. How should I be structuring the formula to make it work correctly?
You need to use parseInt(val, 10) or parseFloat(val) to convert the value of each form field as number. Otherwise, you are just doing string manipulation.

javascript nodejs rounding errors

i want everything to be precise and add up exactly and i can't allow the total to be a penny off.
var buy_amount = 132.32000000; //amount in bitcoin
var buy_amount_satoshi = buy_amount*100000000; //amount in satoshi
var sell_rate = 10.00000000; //USD rate
var spend_usd = buy_amount_satoshi*sell_rate; //total USD
var spend_display = spend_usd/100000000; //total USD user display
console.log(spend_usd.toFixed(8)); //132320000000.00000000
console.log(spend_display.toFixed(8)); //1323.20000000
jsfiddle.net/XjLLS/
multiply the amount with 100000000 to get the no. of satoshis -
Proper Money Handling
multiply the amount with the rate
add 8 decimal places to the result
am i doing it right? i appreciate your help!
UPDATE:
i'm now use the bigdecimal.js library and made the following snippet:
var bigdecimal = require("bigdecimal");
var mode = bigdecimal.RoundingMode.HALF_EVEN(); //default: DOWN
var satoshi = new bigdecimal.BigDecimal("100000000");
var buy_amount = new bigdecimal.BigDecimal("132.32000000"); //amount in bicoin
var amount_minor = buy_amount.multiply(satoshi); //amount in satoshi
var sell_rate = new bigdecimal.BigDecimal("10.00000000"); //sell rate usd
var spend_minor = amount_minor.multiply(sell_rate);
var user_spend = spend_minor.divide(satoshi, 8, mode); //total usd user must spend
var user_display = user_spend.toString();
console.log(user_display);
jsfiddle.net/wwpWA/
the default rounding mode is DOWN but i set it to HALF_EVEN aka banker's rounding (banker’s rounding is common when working with money)
i hope all rounding errors are now gone! please correct me if i am wrong!
try this
var buy_amount = 132.32000000;
var buy_amount_satoshi = (parseFloat(buy_amount)*100000000).toFixed(8);
var sell_rate = 10.00000000;
var spend_satoshi = (parseFloat(buy_amount_satoshi)*parseFloat(sell_rate)).toFixed(8);
var spend_display = (parseFloat(spend_satoshi)/100000000).toFixed(8);

Categories