Hi, this looks like an Espruino issue - maybe not related to map specifically, but it looks like if you break out of a FOR loop then it's possible the iterator still gets called before it exits?
Changing break for return i will fix it:
var lvls = [65535,54038,47824,39322,32768,26526,18388,11818,5958],
halfDiff = 2979; //2958/2
function anaGetKey(samples){
var keys = samples.map(function(v) {
var i;
var val = v + halfDiff;
for (i=0; i<lvls.length; i++) {
if (lvls[i] < val){
console.log("found : "+i+" for "+v+" - "+val);
return i; //<------- here
}
}
console.log(" => return "+i);
return i;
});
console.log(keys);
}
anaGetKey([6000,12000,54000,65000]);
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
Hi, this looks like an Espruino issue - maybe not related to map specifically, but it looks like if you
break
out of a FOR loop then it's possible the iterator still gets called before it exits?Changing
break
forreturn i
will fix it:I'm on holiday next week but i've just filed a bug for this at https://github.com/espruino/Espruino/issues/2012 and I'll get on it when I get back