Karl Morrison Karl Morrison - 4 months ago 27
Javascript Question

Translate row in Python to JavaScript: [(M[i], k // M[i])]

I'm trying to translate the following script into JavaScript. There is one row which I have no idea what it does, I'm suspecting push to the array.

def greedyCoinChanging(M, k):
n = len(M)
result = []
for i in xrange(n - 1, -1, -1):
result += [(M[i], k // M[i])] // <-- what the hell is this in JavaScript?
k %= M[i]
return result

Answer

In JavaScript, you don't have to use a tuple. You can just use an array.

result.push([M[i], Math.floor(k/M[i])]);

Also, the integer division (//) can become Math.floor(k/M[i]);