How To get result in the browser of this code?

6 views
Skip to first unread message

Angeleyes

unread,
Jun 22, 2016, 5:17:59 PM6/22/16
to cnodejs
i'm newbie so i appreciate any help that I can get.

i have a code get result in terminal by console.log

but i want to get results in browser by nodejs,angular or ionic
and every sum in new line separate itemz by comma as screenshot



also, if can get external url json for big dataset like:

Thanks in advance!


here is the code:

function subsetSum(numbers, target, partial) {
  var s, n, remaining;
 partial = partial || [];
 s=0;
 for(var j=0;j<partial.length;j++){
    s=parseInt(partial[j].value)+ s;
  }

  // check if the partial sum is equals to target

  if (s === target) {
   console.log("combination is");
    partial.forEach(function(i){console.log(i.itemz);})

  }

  if (s >= target) {
    return;  // if we reach the number why bother to continue
  }

  for (var i = 0; i < numbers.length; i++) {
    n = numbers[i];
    remaining = numbers.slice(i + 1);
    subsetSum(remaining, target, partial.concat([n]));
  }
}
var obj = [
  { 'itemz': 'tomato', 'value': 5 },
  { 'itemz': 'potatos', 'value': 3 },
  { 'itemz': 'banana', 'value': 7 },
  { 'itemz': 'orange', 'value': 6 },
  { 'itemz': 'apple', 'value': 4 },
  { 'itemz': 'cherries', 'value': 5 },
  { 'itemz': 'watermelon', 'value': 9 },
  { 'itemz': 'orange', 'value': 8 }
];

subsetSum(obj,10);


张小彬

unread,
Jun 29, 2016, 9:58:56 PM6/29/16
to cno...@googlegroups.com
hi~ 



var http = require("http");


var result = [];


function subsetSum(numbers, target, partial) {
var s, n, remaining;
partial = partial || [];
s=0;
for(var j=0;j<partial.length;j++){
s=parseInt(partial[j].value)+ s;
}

// check if the partial sum is equals to target

if (s === target) {
console.log("combination is");
        //add array
result = result.concat(partial);

partial.forEach(function(i){
console.log(i.itemz);
})

}

if (s >= target) {
return; // if we reach the number why bother to continue
}

for (var i = 0; i < numbers.length; i++) {
n = numbers[i];
remaining = numbers.slice(i + 1);
subsetSum(remaining, target, partial.concat([n]));
}
}
var obj = [
{ 'itemz': 'tomato', 'value': 5 },
{ 'itemz': 'potatos', 'value': 3 },
{ 'itemz': 'banana', 'value': 7 },
{ 'itemz': 'orange', 'value': 6 },
{ 'itemz': 'apple', 'value': 4 },
{ 'itemz': 'cherries', 'value': 5 },
{ 'itemz': 'watermelon', 'value': 9 },
{ 'itemz': 'orange', 'value': 8 }
];

subsetSum(obj,10);


http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'text/plain'});
//result to string in browser
response.end(JSON.stringify(result));
}).listen(8888);

console.log('Server running at http://127.0.0.1:8888/');



--
您收到此邮件是因为您订阅了Google网上论坛上的“cnodejs”群组。
要退订此群组并停止接收此群组的电子邮件,请发送电子邮件到cnodejs+u...@googlegroups.com
要查看更多选项,请访问https://groups.google.com/d/optout

Reply all
Reply to author
Forward
0 new messages