So I have a strings like this: '5-2 7-1 8-9 7-4 1-3 1-0 2-8 8-0 6-9', it's always in this form (number, dash, number, space, number, dash, ..)
What I want to do is to transform this in to an array of integers: [5, 2, 7, 1, 8, ..., 9] I had this solution
var str = '5-2 7-1 8-9 7-4 1-3 1-0 2-8 8-0 6-9';
numbers = str.replace('-', ' ').split(' ').map(function(entry) { return parseInt(entry); });
// ==> [ 5, 2, 7, 8, 7, 1, 1, 2, 8, 6 ] WTF!!
So I went with this
var str = '5-2 7-1 8-9 7-4 1-3 1-0 2-8 8-0 6-9';
numbers = str.split(' ').join('').split('-').join('').split('').map(function(num) {
return parseInt(num);
}); // ==> [ 5, 2, 7, 1, 8, 9, 7, 4, 1, 3, 1, 0, 2, 8, 8, 0, 6, 9 ] All good!
But I don't know why the first solution doesn't work, I know the problem is with the str.replace but I can't figure out why it produce this result