Specific situation.. I'm having an array filled with datetimes I pull in via an api.
Users should be able to select a date from a datepicker (only showing dates available in the array) and afterwards see the corresponding time.
So what I've done..
The original array is obtained via php, so before starting to populate the datepicker with possible dates I create an extra array with dates only.
Since I maintain the key's it's possible to put these 2 arrays next to eachother.
Array looks as following:
["8-8-2017,07:00", "26-8-2017,07:00"];
So far so good...
After a user picks a date I trigger this to be able to start digging for the time corresponding that date.
Now it's getting messy...
$('#datepick').datepicker().on("input change", function(e) {
$("#uur").text('');
var selecteddate = e.target.value;
var searchArr = datesArray;
var ind = searchArr.indexOf(selecteddate.toString());
var result = datesArray.filter(function(item) {
return typeof item == 'string' && item.indexOf(selecteddate.toString()) > -1;
});
var afterComma = result.toString().substr(result.toString().indexOf(",") + 1);
var final = afterComma.replace(":", "u");
$("#uur").text("De warming up party gaat van start rond " + final);
});
The result is that this only works on the last element of the array.
Because I'm splitting based on the comma's. Now I know the easiest way to work arround this would be to change the , that's seperating date and time in another symbol but still I'm wondering why this couldn't be easier.
You convert whole array to string every time. You should change following code:
var afterComma = result.toString().substr(result.toString().indexOf(",") + 1);
To this;
var afterComma = item.toString().substr(item.toString().indexOf(",") + 1);
Edit:
I also missed the loop above
//for every item in result, afterComma will refer to related minute string
for (var item in result) {
var afterComma = item.toString().substr(item.toString().indexOf(",") + 1);
// Do rest here
}
Ok, I have been working on this for some time, I have some close to getting it to work but not completely. So what I am doing is adding the value from a weekly input form into an array with its key.
There will be no limit on the number of rows as I can (and this works fine) AJAX add a row to the form with a button.
I currently add all the totals for each day together, this works, as all the Mondays have a .Monday class on them (I can post that code it need, please just ask) and each other day.
I have also got an id on each input which as the day of the week and a count, so #Monday0, #Monday1, same for each day and each row ect.
Now what I am doing with the code below, is to add the week up and then display that (console log for now) in that weeks row. So I want to add all the daily ids, Monday though to Sunday that end in 0, then do the same for 1 and so on.
var LoadHourTotals = function() {
$('.TimebreakdownInput').change(function() {
var InputArrays = []; //Array to store all weekly inputs
var Totals = []; //Store Array total for display
GetCurrentID = $(this).attr('id');
CurrentCount = GetCurrentID.charAt(GetCurrentID.length-1)
var WeeklyArray = ["Monday"+CurrentCount,"Tuesday"+CurrentCount,"Wednesday"+CurrentCount,"Thursday"+CurrentCount,"Friday"+CurrentCount,"Saturday"+CurrentCount,"Sunday"+CurrentCount];
$.each(WeeklyArray, function(k, v) {
var values = parseFloat( $('#'+v).val() );
if (isNaN(values)) { values = 0; } //Set value to 0 if its not a number
if (!values) { values = 0; }
InputArrays.push({ key: CurrentCount, hours:values });
});
console.log(InputArrays);
//$('.TimebreakdownTotalHours').html(Totals); //Display / Add total into HTML
});
} //End of LoadHourTotals function
I think I am close with this, each daily input is saved into its own array with a key count and its value for that day. For example, 0:XX 0:XX (this seven times, all for the 1st row). This is then repeated for each row as needed.
If what I have done is not right or there is a better method for doing this, then please let me know.
But now what I need to do is go though each key, take its value, getting a 'grand' total for all seven inputs, then display or save that total into a new array (which is what I was trying to do) then display / console log each weekly total.
I have gone though a number of posts on here but I could not find anything that fits for my problem.
All help very welcome.
If I have not posted some code that is need then please let me know.
Please let me know if I have not explained myself right.
Many Thanks.
Its ok, I have found an answer. I tried this but it did not work,
var total = 0;
$.each(InputArrays,function() {
total += this;
console.log(total);
});
But some playing around with the code, I console loged 'this' and tried the following which now seems to work. Thanks
var total = 0;
$.each(InputArrays,function() {
total += this.hours;
console.log(total);
});
For this question, it might be a little vague, because i just dont understand it at all, its probably the wording.. from what i learn in class it seems a lot harder. So im lost as to where to begin.. if someone can help walk me through it easier i would appreciate it!
Question: Design a Program that will read the same parts inventory file described in the problem 6. the parts are: (Record code, part number, part description, and inventory balance) validate the record code and part number on each record, and print the details of all valid records whose part numbers fall within the value AA3000 and AA3999 inclusive. Also print a count of these selected records at the end of the parts listing.
Now, i hope you can understand what its asking because i sure dont. Any help or a small walk through would be awesome. This is the code i am supposed to start out from that was given to me.
var Rec_Code = new Array(11,11,11,12,11,12,11,13,11,14);
var Numer = new Array(2000,3000,3003,3008,3999,2000,1090,3678,3777,3543);
var Alpha = new Array("AA","AA","AX","AA","AA","AA","AB","AA","AN","AA");
var Desc = new Array("X","L","S","F","R","U","T","N","Q","Y");
var Inv_Bal = new Array(12,13,14,23,34,56,32,45,67,77);
also, this was given to me, which is basically what i have to do, but dont know how to completely do it.
use the vars that I provided to create 5 parallel arrays, RecCode, AlphaPart of part number, Numeric part of the part number,Description and Inventory. You need to search the first 3 arrays for:
RecCode of 11
AlphaCode of 'AA':
Numeric Code betweewn 3000 - 3999 inclusive
when you find a match increment a count and display the Description and Inventory.
Assuming that all arrays are the same length and sorted appropriately, you can loop over one and display the information you need:
var count = 0;
for(var i = 0; i < Rec_Code.length; i++)
{
if(Rec_Code[i] == 11 && Alpha[i] == 'AA' && (Numer[i] >= 3000 && Numer[i] <= 3999))
{
console.log(Desc[i]);
console.log(Inv_Bal[i]);
count++;
}
}
var Rec_Code = new Array(11,11,11,12,11,12,11,13,11,14);
var Numer = new Array(2000,3000,3003,3008,3999,2000,1090,3678,3777,3543);
var Alpha = new Array("AA","AA","AX","AA","AA","AA","AB","AA","AN","AA");
var Desc = new Array("X","L","S","F","R","U","T","N","Q","Y");
var Inv_Bal = new Array(12,13,14,23,34,56,32,45,67,77);
var count = 0;
for(var i = 0; i < Rec_Code.length; i++)
{
if(Rec_Code[i] == 11 && Alpha[i] == 'AA' && (Numer[i] >= 3000 && Numer[i] <= 3999))
{
console.log(Desc[i]);
console.log(Inv_Bal[i]);
count++;
}
}
Is there an API to count the number of Tweets since 0:00am this morning for a particular user? I have tried the following Javascript but the closest I can get to it is the total number of tweets for all time for that user.
$.getJSON("http://api.twitter.com/1/statuses/user_timeline/BarackObama.json?count=1&include_rts=1&callback=?", function(data) {
$("#twitter").html(data[0].user.statuses_count);
});
You can download the user timeline until you get tweets which were posted "yesterday" (i.e. before 0:00am). Once you get it, you just have to count tweets which were posted "today" (i.e. after 0:00am).
EDIT 1 : a pseudo JavaScript code for getting it
var howManyTweetsWerePostedToday = function () {
var timeline = downloadTimeline()
var lastTweet = timeline[timeline.length-1]
var now = new Date()
var today = new Date(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDay(), 0, 0, 0, 0) // Limit between today and yesterday
var lastTweetDate = new Date(lastTweet["created_at"])
while (lastTweetDate.getTime() >= today.getTime()) {
var lastTweetID = lastTweet["id_str"]
var earlierTweetsTimeline = downloadTimeline(max_id = lastTweetID)
timeline = timeline.concat(earlierTweetsTimeline.shift())
lastTweet = timeline[timeline.length-1]
lastTweetDate = new Date(lastTweet["created_at"])
}
return getNumberOfTweetsThatWerePostedTodayInTheTimeline(timeline)
}();
With downloadTimeline() which is a function calling the GET statuses/user_timelineTwitter API endpoint to get the timeline. See https://dev.twitter.com/docs/api/1/get/statuses/user_timeline for details about the endpoint, especially max_id which is the highest tweet ID that will be in the results.
created_at is the date when a tweet was posted. id_str is the ID of a tweet under a String form. See https://dev.twitter.com/docs/platform-objects/tweets for more details about tweets.
I have an array that has 30 date objects. The date objects are indexed in the array from the minimum date value to the maximum date value. What I would like to do is retrieve only 7 dates from the array. Out of the 7, the first one should be the minDate and the last should be the maxDate, with 5 dates in the middle. The 7 numbers should increment evenly from the minDate to the maxDate. How would I accomplish this? Hope I was clear.
Thanks,
Tonih
well if you were trying to evenly distribute by date then make sure all your objects are in the date class then do array[29].getTime()-array[0].getTime() /7 for your average step, then do something like array.forEach() with a comparason function to try and get the closest to each step.
--edit--
try something like:
//dateArray is array of dates
var targetTime:Number;
var filteredarray:Array = new Array();
var step = dateArray[29].getTime()-dateArray[0].getTime() /7
var smallestdist:Number;
var currentIndex:int;
filteredarray.push(dateArray[0]); //Add the first entry
targetTime = dateArray[29].getTime(); //set the lowest point
for(var i=1; i<7; i++){ //loop 6 more times
smallestdist = Number.POSITIVE_INFINITY; //set a large smalldist
currentIndex = 0; //Set a small index
targetTime += step; //increment the target time
dateArray.forEach(testDate); //loop through the array and test with testDate function
filteredarray[i] = dateArray[currentIndex] //Add the result to the dateArray
}
function testDate(item:Date, index:int, array:Array){
//Check the absolute value against current stored distance
if(Math.abs(item.getTime() - targetTime) < smallestdist){
//if less then set this as new target
smallestdist = Math.abs(item.getTime() - targetTime);
currentIndex = index;
}
}
of course this is dealing with a preumed even spread of dates, there could be the posibility of adding the same date to several different points if all of dateArray are clumped together, could be optimised, but see what you can do with it.
i havnt tested this code, but it should work pretty out of the box. have a look at these if you have a problem:
Array::forEach()
Date::getTime()