My coding question involves a function that calculates the product of numbers in an array. The way this function is supposed to work is like this:
function calculateProduct(array) {
// Compute and return product
}
var arr = [1, 2, 3, 0, 4, 5, 0, 6, 7, 8, 0, 9];
The function call:
calculateProduct(arr); // Should return 6
calculateProduct(arr); // Should return 20
calculateProduct(arr); // Should return 336 (6 * 7 * 8)
calculateProduct(arr); // Should return 9
calculateProduct(arr); // Should return 0
calculateProduct(arr); // Should return 0
calculateProduct(arr); // Should return 0
In Scheme programming language, achieving this functionality is done using continuations by storing the previous state of the function just before its exit point. You can see more about it here.
Therefore, I am looking for a solution in JavaScript where the function can return different values at different times with the same parameter passed every time.
I trust that JavaScript's flexibility as a well-designed language may provide a way to achieve this functionality. However, if there is no such capability in JS, I am prepared to accept that outcome and move forward. Feel free to let me know if you believe it's impossible.
Thank you.