Jensen's Device: Difference between revisions

no edit summary
No edit summary
No edit summary
Line 434:
 
And another obvious variation here would be turning the expression into a named entity (if it has some lasting usefulness).
 
=={{header|JavaScript}}==
{{trans|C}}
 
Uses an object ''o'' instead of integer pointer ''i'', as the C example does.
 
<lang javascript>var obj;
 
function sum(o, lo, hi, term) {
var tmp = 0;
for (o.val = lo; o.val <= hi; o.val++)
tmp += term();
return tmp;
 
obj = {val: 0};
alert(sum(obj, 1, 100, function() {return 1 / obj.val}));</lang>
The alert shows us '5.187377517639621'.
 
=={{header|Java}}==
Line 470 ⟶ 452:
</lang>
The program prints '5.17737751763962'.
 
=={{header|JavaScript}}==
{{trans|C}}
 
Uses an object ''o'' instead of integer pointer ''i'', as the C example does.
 
<lang javascript>var obj;
 
function sum(o, lo, hi, term) {
var tmp = 0;
for (o.val = lo; o.val <= hi; o.val++)
tmp += term();
return tmp;
 
obj = {val: 0};
alert(sum(obj, 1, 100, function() {return 1 / obj.val}));</lang>
The alert shows us '5.187377517639621'.
 
=={{header|Joy}}==