I am transitioning a project from Python to Node.js - everything has been going fine up until this point. I have a function that is supposed to convert the amount of EXP someone has into a level. I have attempted to rewrite the working function from Python into Node.js but the functions return completely different answers and I can't figure out why.
Python:
def calculateLevel(exp):
if exp < 83: return 1
else:
calcExp = 0
for level in range(1, 1000):
diff = int(level + 300 * math.pow(2, float(level)/7))
calcExp += diff
if exp < calcExp//4:
break
return level
Node.js:
const _ = require("underscore");
// ...
function calculateLevel(exp) {
return new Promise((resolve, reject) => {
if (exp < 83) resolve(1);
else {
var calcExp = 0;
for (var i in _.range(1, 1000)) {
calcExp += parseInt(i + 300 * Math.pow(2, i/7));
if (exp < Math.floor(calcExp/4)) {
resolve(i);
break;
}
}
}
});
}
For example, calculateLevel(123456) in Python returns 51 (the correct answer), but await calculateLevel(123456); in Node.js returns 15 (the incorrect answer).
Clearly there is something wrong in the Node.js version, but I can't figure out what it is.
Any help is greatly appreciated. Thanks in advance.
resolve(i)is not in the same block than yourreturn level, maybe you need to get it out of the loop