How to find all instances between 2 variabels [duplicate] - javascript
This question already has answers here:
Sort array by distance near user location from firebase
(1 answer)
Query for nearby locations
(1 answer)
Closed 17 days ago.
i'm using firebase to save stores and you will be able to select a range and then it should find all locations in this range. My current code is:
function search(){
//converts address to coordinates
var address = addres;
var lng = 0;
var lat = 0;
//calculates ranges
var range = document.getElementById("range").value;
var maxlon = lng + range;
var minlon = lng - range;
var maxlat = lat + range;
var minlat = lat - range;
//biermerken
var score = document.getElementById("alfa").checked;
var beer = document.getElementById("amstel").checked;
var school = document.getElementById("bavaria").checked;
var leffe = document.getElementById("leffe").checked;
It should first search between max lon and min lon for example and then max lat and min lat. And then it should check if for example everything except score is checked off it should only display items where score is turned on. I can't seem to find anything in the firebase docs or anything...
To be able to search for more than one item in the firebase
Related
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');
Getting the value in float even though input value is integer
I am reading the data from Google spread sheet using Google app script. in one of the cell(sprint version) of spreadsheet I have mentioned the value as 56 but when I am reading the value it is returning 56.0. I don't know why Below method, I wrote to read the value from spreadsheet function readAndSetTimePeriodMetaData() { var timePeriodArray = new Array(); var masterSheetID = "My Google Sheet ID"; var masterSheet = SpreadsheetApp.openById(masterSheetID); var timePeriodDataSheet = masterSheet.getSheetByName("Time Period MetaData"); data = timePeriodDataSheet.getDataRange().getValues(); var range = timePeriodDataSheet.getRange(1,2); var sprintVersion = range.getValue(); //it is returning 56.0 var range = timePeriodDataSheet.getRange(2,2); var monthValue = range.getValue(); var range = timePeriodDataSheet.getRange(3,2); var quarterValue = range.getValue(); Logger.log(sprintVersion) }
I solved it by using toFixed(0) var sprintVersion = range.getValue().toFixed(0);
How to deep learn from a row of numbers using Node.js and convnetjs and predicted a new value?
I try to to use convnetjs to make Node.js learn from a row of numbers in x,y coordiinates. The goal is to predicted next value in a simple number row. First of all a very simple row [0,1,0,2,0,3,0,4,0,5,0,6] maybe later sin and cos number row. I do not want to go to deep into the deep learning materia so I am using convnetjs. So far I tried : var convnetjs = require("./convnet-min.js"); // create a net out of it var net = new convnetjs.Net(); var layer_defs = []; layer_defs.push({type:'input', out_sx:1, out_sy:1, out_depth:1}); layer_defs.push({type:'fc', num_neurons:5, activation:'sigmoid'}); layer_defs.push({type:'regression', num_neurons:1}); var net = new convnetjs.Net(); net.makeLayers(layer_defs); var my_data = [ 0,1,2,3,4,5,6,7,8,9,10, 0,1,2,3,4,5,6,7,8,9,10, 0,1,2,3,4,5,6,7,8,9,10, 0,1,2,3,4,5,6,7,8,9,10, 0,1,2,3,4,5,6,7,8,9,10, 0,1,2,3,4,5,6,7,8,9,10, 0,1,2,3,4,5,6,7,8,9,10, 0,1,2,3,4,5,6,7,8 ]; var x = new convnetjs.Vol(my_data); var trainer = new convnetjs.SGDTrainer(net, {learning_rate:1.1, momentum:0.0, batch_size:1, l2_decay:0.001}); var think = function () { for (var i = 0; i < my_data.length; i++) { x.w[i] = my_data[i]; // Vol.w is just a list, it holds your data trainer.train(x, my_data[i]); } } for (var i = 0; i < 100; i++) { think(); var predicted_values = net.forward(x); console.log('predicted value: ' + predicted_values.w[0]); } To realize learning I want to predict the next value, but I wonder (knowing the next value [9]) how to tell the trainer he did a bad, good or very good job? What this the right way to train x more to predict a value? I guess this is not trivial as that because the predicted value goes not into direction of value 9 ^^.
You need to define a domain space input for your data. After that follow this steps: Create a network according to the domain previously defined (please read this documentation to tune the parameter in the network: convnetjs-doc). Train the network, please also refer to the convnetjs-doc in order of select proper values for the parameters of the trainer. The follow example show you a network assuming that the domain space is 9 (the network must predict the next value for one row of size 9). I'm using the same data set for training (my_data), so to meet the domain space requirement in each data item I'm taking arrays of size 9 in each step in the training process from my_data (using the slice function) and assuming that the real value for each row is the next value in my_data after take an array of size 9 (if the data set change you should take a different approach to create items that meet the same domain space requirement). The function learn do the learning process described above, var data = my_data.slice(i, i + d); take an array of size d (9 in this example) from my_data starting at i, so we are moving through the data training set and taking slices of size 9 (to meet the domain space requirement). After that we get the real value for data with this: var real_value = [my_data[i + d]]; which is the value next to the last in data, note that since we are working with regression the real_value must be a LIST (see convnetjs-doc for more details). Then we create a Vol class var x = new convnetjs.Vol(data); to storage the data, and finally we train the net setting the real value real_value for the previously created Vol class trainer.train(x, real_value);. When we finish the learning process we are ready for predict some values, all we have to do is create a new input, using a Vol class and predict with the trained net. This is the code: var convnetjs = require('convnetjs'); // create a net out of it var net = new convnetjs.Net(); var d = 9; var layer_defs = []; layer_defs.push({type:'input', out_sx:1, out_sy:1, out_depth:d}); layer_defs.push({type:'fc', num_neurons:10, activation:'sigmoid'}); layer_defs.push({type:'regression', num_neurons:1}); var net = new convnetjs.Net(); net.makeLayers(layer_defs); var my_data = [ 0,1,2,3,4,5,6,7,8,9,10, 0,1,2,3,4,5,6,7,8,9,10, 0,1,2,3,4,5,6,7,8,9,10, 0,1,2,3,4,5,6,7,8,9,10, 0,1,2,3,4,5,6,7,8,9,10, 0,1,2,3,4,5,6,7,8,9,10, 0,1,2,3,4,5,6,7,8,9,10, 0,1,2,3,4,5,6,7,8 ]; var trainer = new convnetjs.SGDTrainer(net, {learning_rate:0.01, momentum:0.2, batch_size:1, l2_decay:0.001}); var learn = function () { for(var j = 0; j < 100; j++){ for (var i = 0; i < my_data.length - d; i++) { var data = my_data.slice(i, i + d); var real_value = [my_data[i + d]]; var x = new convnetjs.Vol(data); trainer.train(x, real_value); var predicted_values = net.forward(x); console.log("data: [" + data + "] -> value: " + real_value); console.log("prediction in learn stage is: " + predicted_values.w[0]); } } } var predict = function(data){ var x = new convnetjs.Vol(data); var predicted_value = net.forward(x); return predicted_value.w[0]; } learn(); var item = [0,1,2,3,4,5,6,7,8]; console.log("predicted value for [" + item + "] is: " + predict(item)); These are some example output: predicted value for [3,4,5,6,7,8,9,10,0] is: 1.0789064579041727 predicted value for [0,1,2,3,4,5,6,7,8] is: 9.223386915148865 predicted value for [10,0,1,2,3,4,5,6,7] is: 8.430232430080627 predicted value for [1,2,3,4,5,6,7,8,9] is: 9.020852169040044 predicted value for [5,6,7,8,9,10,0,1,2] is: 3.0623065881421674 predicted value for [4,5,6,7,8,9,10,0,1] is: 2.208646113846295
Multiply a value based on a select box options
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); } }
How to add hours from 2 cells?
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)); }