javascript - recursion functional programming dilemma -


below code in javascript. loop answer down @ bottom correct answer.

my recursion version incorrect , illogical when index 3 , array length 3 computer still enter if statment... why? if 3 not less 3, computer should not enter if statement. me fix recursion answer. loop correct , fine , should left alone.

var data = [    {        name: "jamestown",      population: 2047,      temperatures: [-34, 67, 101, 87]    },  {    name: "awesome town",    population: 3568,    temperatures: [-3, 4, 9, 12]  }, {   name: "funky town",   population: 1000000,   temperatures: [75, 75, 75, 75, 75] } ]; 

recursion answer

function john( arr, i, j, total, coord) {     var total = total || 0;    var coord = coord || [];    if(i < arr.length)   {     if(j < arr[i].temperatures.length)    {      total = arr[i].temperatures[j] + total;      john(arr, i, j + 1, total, coord)    }       ave = total / arr[i].temperatures.length;      total = 0;      coord.push([ave, arr[i].population])       john(arr, + 1, 0, total, coord)      }     return coord;   }  console.log("recursion answer(it wrong) is") console.log( john(data, 0, 0, 0)) 

loop answer

var coords = [],    totaltemperature = 0,    averagetemperature = 0;  (var i=0; < data.length; i++) {   totaltemperature = 0;   (var j=0; j < data[i].temperatures.length; j++) {    totaltemperature += data[i].temperatures[j];  }    averagetemperature = totaltemperature / data[i].temperatures.length;    coords.push([averagetemperature, data[i].population]); }  console.log("correct answer loop answer ...") console.log(coords) 

you can't replace nested loop 2 recursive calls same function.

when call function i, j + 1 won't calls following values j, calls following values i. when call i + 1, j won't calls following vales i, calls following values j.

instead of getting chain of calls looks this:

0,0   0,1     0,2       0,3   1,0     1,1       1,2         1,3     2,0       2,1         2,2           2,3             2,4 

you looks this:

0,0   0,1     0,2       0,3         0,4           1,0             1,1               1,2                 1,3                   2,0                     2,1                       2,2                         2,3                           2,4                 2,0                   2,1                     2,2                       2,3                         2,4               2,0                 2,1                   2,2                     2,3                       2,4             2,0               2,1                 2,2                   2,3                     2,4         1,0           1,1             1,2               1,3                 2,0                   2,1                     2,2                       2,3                         2,4               2,0                 2,1                   2,2                     2,3                       2,4             2,0               2,1                 2,2                   2,3                     2,4           2,0             2,1               2,2                 2,3                   2,4       1,0         1,1           1,2             1,3               2,0                 2,1                   2,2                     2,3                       2,4             2,0               2,1                 2,2                   2,3                     2,4           2,0             2,1               2,2                 2,3                   2,4         2,0           2,1             2,2               2,3                 2,4     1,0       1,1         1,2           1,3             2,0               2,1                 2,2                   2,3                     2,4           2,0             2,1               2,2                 2,3                   2,4         2,0           2,1             2,2               2,3                 2,4       2,0         2,1           2,2             2,3               2,4   1,0     1,1       1,2         1,3           2,0             2,1               2,2                 2,3                   2,4         2,0           2,1             2,2               2,3                 2,4       2,0         2,1           2,2             2,3               2,4     2,0       2,1         2,2           2,3             2,4 

also, instead of pushing result @ end of each range of j values, push result every single call.


Comments

Popular posts from this blog

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

java - Could not locate OpenAL library -

sorting - opencl Bitonic sort with 64 bits keys -