0

I have the following JavaScript syntax:

console.log(printClassified(classifyByDayOfWeek(myData)));
mySundaysData = new Array(printClassified(classifyByDayOfWeek(myData)));
console.log(mySundaysData);
var mySundaysTotal = 0;
for(var i=0, len=mySundaysData.length; i<len; i++){
    mySundaysTotal += parseFloat(mySundaysData[i][1]);
}
console.log(mySundaysTotal);

Google Chrome console:

printClassified(classifyByDayOfWeek(myData)) = [2013-04-21, 1][2013-04-28, 0][2013-05-05, 2][2013-05-12, 0]
mySundaysData = ["[2013-04-21, 1][2013-04-28, 0][2013-05-05, 2][2013-05-12, 0]"]
mySundaysTotal = 2

I want to count the values from the arrays, for the above example: 1 + 0 + 2 + 0 and it should give me 3 and instead it gives me 2 like it can be saw in the console.log(mySundaysTotal). Even if I got other arrays mySundaysTotal is always 2. Why? What am I do wrong?

EDIT: Rest of the code:

function sorter(a, b) {
    var d1 = new Date(a[0].replace("-", "/")).getDay();
    var d2 = new Date(b[0].replace("-", "/")).getDay();
    return d1 - d2;
}
function classifyByDayOfWeek(customArr) {
    var byDayOfWeek = [[], [], [], [], [], [], []];
    for (var i = 0; i < customArr.length; i++) {
        var day = new Date(customArr[i][0]).getDay();
        byDayOfWeek[day].push(customArr[i]);
    };
    return byDayOfWeek;
}
function printRaw(arr) {
    str = "";
    for (var i = 0; i < arr.length; i++) {
        str += "[" + arr[i][0] + ", " + arr[i][1] + "]";
    }
    return str;
}
function printClassified(arr) {
    str = "";
    str += printRaw(arr[0]);
    return str;
}

JSFiddle - http://jsfiddle.net/DanielaVaduva/NUHFu/

6
  • @PSR - in the initialisation part of the for loop, len is set to the length of the array (which, in this case is 1) Commented May 17, 2013 at 9:32
  • @alex23 I've tried it to see if it makes a difference or not... Commented May 17, 2013 at 9:34
  • Why you don't create this exercise on jsfiddle.net ? Commented May 17, 2013 at 9:38
  • @MirkoCianfarani I will do it right now :D. EDIT - jsfiddle.net/DanielaVaduva/NUHFu Commented May 17, 2013 at 9:40
  • 1
    I find the variable... Commented May 17, 2013 at 9:50

1 Answer 1

1

Ok, I think I have are refined answer. The parsing could do with some work, but:

var raw = "\"['2013-04-15', 26]\", \"['2013-04-16', 10]\", \"['2013-04-17', 51]\", \"['2013-04-18', 46]\", \"['2013-04-19', 32]\", \"['2013-04-20', 50]\", \"['2013-04-21', 26]\", \"['2013-04-22', 31]\", \"['2013-04-23', 48]\", \"['2013-04-24', 821]\", \"['2013-04-25', 917]\", \"['2013-04-26', 949]\", \"['2013-04-27', 405]\", \"['2013-04-28', 593]\", \"['2013-04-29', 925]\", \"['2013-04-30', 877]\", \"['2013-05-01', 277]\", \"['2013-05-02', 112]\", \"['2013-05-03', 115]\", \"['2013-05-04', 62]\", \"['2013-05-05', 74]\", \"['2013-05-06', 76]\", \"['2013-05-07', 51]\", \"['2013-05-08', 93]\", \"['2013-05-09', 231]\", \"['2013-05-10', 350]\", \"['2013-05-11', 258]\", \"['2013-05-12', 0]\", \"['2013-05-13', 61]\"";

var json = raw.replace(/"/g,'');
json = "[" + json + "]";
json = json.replace(/'/g,'"');
myData = [];
var parsed = JSON.parse(json);
for(var i = 0, len=parsed.length; i < len; ++i ) {

    var date = new Date(parsed[i][0]); 
    myData.push( {
        date: date,
        day: date.getDay(),
        count: parsed[i][1]
    });
}

var mySundaysTotal = myData.
    filter(function(x) { return x.day == 0; }). //find sundays
    map(function(x) { return x.count; }). //get an array of the counts
    reduce(function(x,y) { return x+y; }, 0);  //sum the results

console.log(mySundaysTotal);

see it in action: http://jsfiddle.net/tztLe/1/

Sign up to request clarification or add additional context in comments.

1 Comment

And how can I get an array of arrays from that?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.