I have a small form with fixed costs based on where they are shipping from and going to with how many pallets.
For example UK Zone 1 to France Zone 1 = 20
Also UK Zone 3 to France Zone 4 = 68
var values = [
[20,25,35,40],
[36,42,50,56],
[42,56,52,68],
[60,70,68,72]
];
What i'm trying to achieve now is how would I multiply that total value.
So for example if the user selects UK Zone 1 going to France Zone 1 that is = £20 for 1 product
But if they select 2 from the select box the total cost should now be £40
Here is where I have got to but I cant get it to work
function updateValue() {
var fromCountry = document.querySelector('input[name="from_country"]:checked').value;
var toCountry = document.querySelector('input[name="to_country"]:checked').value;
var totalValues = values[fromCountry-1][toCountry-1];
var fixValues = document.querySelector('select[name="number"]');
var n = parseInt(fixValues.val(), 10);
if(fromCountry && toCountry) {
document.getElementById('cost').value = (totalValues * n);
}
}
Full Fiddle Here - http://jsfiddle.net/barrycorrigan/ZXHbq/3/
Help badly needed :-)
val() is jQuery function. Since you don't use jQuery, use fixValues.value. And don't forget to make a call updateValue(), which is missing in your fiddle.
This is the code I used to get it working:
function updateValue() {
var fromCountry = document.querySelector('input[name="from_country"]:checked').value;
var toCountry = document.querySelector('input[name="to_country"]:checked').value;
var totalValues = values[fromCountry-1][toCountry-1];
var fixValues = document.querySelector('select[name="number"]');
var n = parseInt(fixValues.value, 10);
if(fromCountry && toCountry) {
document.getElementById('cost').value = (totalValues * n);
}
}
Related
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');
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).
**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));
}
I am trying to write some javascript to be able to read the date from one column of a SharePoint List. For example if in my date column (MonthNeeded) the date is 03/15 I want to be able to check if the date is < 30 days from today's date or <= 90 days from today's date and so on. When it checks this it will change the row color that corresponds with that date a certain color. Less than 30 days from today it will be red, greater than or equal to 30 days it will be orange, etc...
I wrote some code in Visual Studio because I unfortunately cannot use SharePoint designer with my instance of SP so I can't test my code real time.
Here is what I have so far:
function CheckStatus() {
var x = document.getElementsByTagName("TD") // find all of the TDs
var _dueDate = new Date(ctx.CurrentItem.MonthNeeded)
var now = Date();
var nowPlus30 = new Date();
nowPlus.setDate(now.getDate() + 30);
var nowPlus90 = new Date();
nowPlus.setDate(now.getDate() + 90);
if (_dueDate == '' || !_dueDate) {
return '';
}
var i = 0;
for (i = 0; i < x.length; i++) {
if (x[i].className == "ms-vb2") //find the TDs styled for lists
{
if( _dueDate <= nowPlus30 ) //find the data to use to determine the color
{
x[i].parentNode.style.backgroundColor = 'orange'; // set the color
}
//repeat the above for each data value
if (_dueDate <= nowPlus90) {
x[i].parentNode.style.backgroundColor = 'yellow'; // set the color
}
if (_dueDate > nowPlus30) {
x[i].parentNode.style.backgroundColor = 'red'; // set the color
}
}
}
}
Any help would be greatly appreciated!
I've used the date.js library in the past to do conditional formatting see: http://code.google.com/p/datejs/
Here's a snippet of my code:
var created = oListItem.get_item('Created').toLocaleDateString();
var dateCreated = Date.parse(created);
var lastWeek = Date.today().add(-7).days();
var newIcon = '';
if (dateCreated >= lastWeek) {
newIcon = "<span class='newIcon'> new!</span>";
}
Your code doesn't run properly because you have some minor errors in your JS.
First, check that nowPlus is not defined, however you are trying to use it, and the function breaks, trowing an exception.
You should be using nowPlus30 and nowPlus90 when setting the date plus 30 days, and plus 90 days, respectively.
Also, check the line:
var now = Date();
You need to replace the above line with:
var now = new Date();
As Date is a JS object that needs to be initialized before using it.
Please check out the following fiddle for a reference :)
http://jsfiddle.net/canolucas/gmw9tr5h/
I have some google spreadsheet logbook where I store duration of some activities in hours format [[HH]:MM:SS]. The spreadsheet adds such cells with no issues. However when I try to add them via Google Script I get some garbage. What I found is that Date() object is implicitly created for such cells, but I cannot find API of that value type.
I know I can convert the data to "hour integers" by multiplying them by 24 but that is a nasty workaround as it demands duplication of many cells. I would rather like a solution that will allow to do that in google script itself.
here is a working function that does the trick.
I first tried to format it as a date but 36 hours is not really standard !! so I did a little bit of math :-) )
To get it working you should set a cell somewhere with value 00:00:00 that we will use as a reference date in spreadsheet standard. in my code it is cell D1(see comment in code, reference date in SS is in 1900 and in Javascript is in 1970 ... that's why it is a negative constant of 70 years in milliseconds...)
here is the code and below a screen capture of the test sheet + the logger
It would be a good idea to modify this code to make it a function that takes cell value as parameter and returns the result as an array for example ([h,m,s] or something similar), this code is only to show how it works.
function addHoursValues() {
var sh = SpreadsheetApp.getActive()
var hours1 = sh.getRange('A1').getValue();
var hours2 = sh.getRange('B1').getValue();
var ref = sh.getRange('D1').getValue().getTime();
//var ref = -2209161600000 // you could also use this but it would be less obvious what it really does ;-)
Logger.log(ref+' = ref');
var h1 = parseInt((hours1.getTime()/3600000)-ref/3600000);
var h2 = parseInt((hours2.getTime()/3600000)-ref/3600000);
Logger.log(h1+' + '+h2+' = '+(h1+h2))
var m1 = parseInt((hours1.getTime()-h1*3600000-ref)/60000);
var m2 = parseInt((hours2.getTime()-h2*3600000-ref)/60000);
Logger.log(m1+' + '+m2+' = '+(m1+m2))
var s1 = parseInt((hours1.getTime()-h1*3600000-m1*60000-ref)/1000);
var s2 = parseInt((hours2.getTime()-h2*3600000-m2*60000-ref)/1000);
Logger.log(s1+' + '+s2+' = '+(s1+s2))
var ts=s1+s2
var tm=m1+m2
var th=h1+h2
if(ts>59){ts=ts-60;tm++};
if(tm>59){tm=tm-60;th++}
Logger.log('sum = '+th+':'+tm+':'+ts)
}
EDIT : here are 2 "function" versions with corresponding test functions that show how to use it
function getHMS(hrs) {
var t = hrs.getTime()/1000;
var ref = -2209161600;
var h = parseInt((t-ref)/3600);
var m = parseInt((t-h*3600-ref)/60);
var s = parseInt(t-h*3600-m*60-ref);
return[h,m,s];// returns an array of 3 discrete values
}
function testHMS(){
var sh = SpreadsheetApp.getActive();
var hours1 = sh.getRange('A1').getValue();
var hours2 = sh.getRange('B1').getValue();
var sumS = getHMS(hours1)[2]+getHMS(hours2)[2];// add seconds
var sumM = getHMS(hours1)[1]+getHMS(hours2)[1];// add minutes
var sumH = getHMS(hours1)[0]+getHMS(hours2)[0];// add hours
if(sumS>59){sumS=sumS-60 ; sumM++}; // handles values >59
if(sumM>59){sumM=sumM-60 ; sumH++}; // handles values >59
Logger.log(sumH+':'+sumM+':'+sumS);
}
OR
function addHMS(hrs1,hrs2) {
var t1 = hrs1.getTime()/1000;
var t2 = hrs2.getTime()/1000;
var ref = -2209161600;
var h = parseInt((t1-ref)/3600)+parseInt((t2-ref)/3600);
var m = parseInt((t1-parseInt((t1-ref)/3600)*3600-ref)/60)+parseInt((t2-parseInt((t2-ref)/3600)*3600-ref)/60);
var s = parseInt(t1-parseInt((t1-ref)/3600)*3600-parseInt((t1-parseInt((t1-ref)/3600)*3600-ref)/60)*60-ref)
+parseInt(t2-parseInt((t2-ref)/3600)*3600-parseInt((t2-parseInt((t2-ref)/3600)*3600-ref)/60)*60-ref);
if(s>59){s=s-60 ; m++}; // handles values >59
if(m>59){m=m-60 ; h++}; // handles values >59
return[h,m,s];// returns sum in an array of 3 discrete values
}
function othertestHMS(){
var sh = SpreadsheetApp.getActive();
var hours1 = sh.getRange('A1').getValue();
var hours2 = sh.getRange('B1').getValue();
Logger.log(addHMS(hours1,hours2));
}