Created
April 5, 2012 22:02
-
-
Save motiooon/2314528 to your computer and use it in GitHub Desktop.
array sum 10
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var a1 = { | |
"apple":9, | |
"pear":3, | |
"glos":8, | |
"mark":6, | |
"gogo":2, | |
"lulu":5 | |
} | |
var a2 = []; | |
for (var key in a1){ | |
var interim = { | |
key: key, | |
value: a1[key] | |
} | |
a2.push(interim); | |
} | |
function sums10(a,index){ | |
var ar =[]; | |
var s=0; | |
for(var i=index; i<a.length; i++){ | |
if(s+a[i]["value"]==10) { | |
ar.push(a[i]["key"]); | |
return console.log(ar); | |
}else if (s+a[i]["value"]<10){ | |
s=a[i]["value"]; | |
ar.push(a[i]["key"]); | |
for (var j =i+1; j<a.length; j++){ | |
if(s + a[j]["value"] == 10){ | |
ar.push(a[j]["key"]); | |
return console.log(ar); | |
}else if(s + a[j]["value"] < 10){ | |
ar.push(a[j]["key"]); | |
s = s + a[j]["value"]; | |
} | |
} | |
} | |
} | |
if(s!=10){ | |
sums10(a,index+1); | |
}else{ | |
return console.log(ar); | |
} | |
if(index+1 > a.length && s!=10){ | |
return console.log("no sum of elem match 10"); | |
} | |
} | |
sums10(a2,0); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment