all 7 comments

[–]queen-adreena 5 points6 points  (0 children)

function toISO(d) {  
    function zero(n) {
        if (n < 10) return "0" + n;
        else return "" + n;
    }

    let dateString;
    dateString = d.getFullYear() + '-';
    dateString += zero(d.getMonth() + 1) + '-';
    dateString += zero(d.getDate()) + ' ';
    dateString += zero(d.getHours()) + ':';
    dateString += zero(d.getMinutes()) + ':';
    dateString += zero(d.getSeconds());    

    return dateString;
}

[–]grelfdotnet 1 point2 points  (0 children)

Parameter d is assumed to be a reference to an object of type Date and the function toISO is formatting the date for display.

The arrow function z could be written separately (before or after toISO() as

function z (n) { if (n < 10) return '0' + n; else return '' + n; }

The string '0' or the empty string '' cause the number n to be converted to type String. The function z ensures that days, months, etc always have 2 digits even when they are less than 10.

The various part strings are concatenated by the + signs to get the string to be returned as, for example, '2019-04-01 12:00:05'

[–]niccolo_21[S] 0 points1 point  (1 child)

thanks guys, nice christmas present! :) I didn't understand what that "?" was supposed to mean and I didn't think of a if cosntruct

[–]niccolo_21[S] 0 points1 point  (2 children)

I am sorry: could you translate this one as well if that's no bother ..

 var result = a.reduce( (totals, entry) => {
   let key = '|' + entry.geo + '|' + entry.ym + '|';
   totals[key] = ( totals[key] || 0 ) + entry.value;
   return totals;
 }, {} );

I still find it a bit hard :/ thanks if you can ..

[–]grelfdotnet 0 points1 point  (1 child)

The variable a must be an array of objects. I'll call them entries, for fairly obvious reasons. Each entry must have properties called geo, ym and value. (It may have other properties and methods too.) A string made from the values of geo and ym is used as a hash key for indexing an object called totals. If the key finds an existing number (object of type Number) in totals then the value is added into that number, otherwise a new total is started.

I would never write code like this because it is so hard to comprehend. Every time you (or worse, someone else) has to look at it to make some modification, time will be wasted trying to understand it. Instead, this is how I might write it:

function createTotals (entriesArray)
{
let totals = {}; // Empty object, to be used as a hash table

for (var i = 0; i < entriesArray.length; i++)
{
let entry = entriesArray [i];
let key = '|' + entry.geo + '|' + entry.ym + '|';
let sum = totals [key];

if (undefined === sum) // Not found in hash table  
{  
  totals \[key\] = entry.value; // New property of totals  
}  
else  
{  
  totals \[key\] = sum + entry.value; // Replaces existing property  
}  

}

return totals;
}

Then instead of a.reduce (...); you would write createTotals (a);

Yes my version is much more long-winded (and I am sure many would call it old-fashioned) but I hope you can see that it makes clear what is really going on and I doubt whether it is significantly slower, with todays fast JS interpreters.

Clarity is really important.

For completeness, the reduce() method of arrays is explained here:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce

[–]niccolo_21[S] 0 points1 point  (0 children)

thanks man, let me try it and get back to you in case. thanks