My case is to separate a string into array of strings and then convert every three characters into a string. ( e.g. "xxxyyy" -> ['xxx','yyy'] )
const translate = function (RNA) {
var arrRna = Array.from(RNA);
var arr = [];
for (var key in arrRna) {
if ((key % 3) == 0) {
var temp = RNA.slice( key, (key+3));
arr.push(temp);
}
}
return arr;
}
console.log(translate('xxxyyyzzz'));
Expected result : ['xxx','yyy','zzz']
But the result that I want is : [ 'xxx', 'yyyzzz', 'zzz' ]
Also, I noticed that the slice method works as expected in first iteration but after that, the weird result --> 'yyyzzz'. Why??
for...inloops on arrays, that's generally unreliable given the (unfortunately) common practice of adding enumerable methods to built-in types (and the fact that the keys are strings instead of numbers as pointed out in the answer below).