'Codewars 6kyu - Data Reverse
There's a task. A stream of data is received and needs to be reversed. Each segment is 8 bits long, meaning the order of these segments needs to be reversed, for example:
11111111 00000000 00001111 10101010
should become:
10101010 00001111 00000000 11111111
I didn't understand the task and to solve the problem, I decide to make an object. Then make an array and transform input. Then sort y and get the result (In my head, it should work so, but...). But sometimes I get it, and sometimes forEach return undefined, and I don't know why? Where am I wrong? And why? I understand that this is not good way for solving problem. I find another way, but I want to know why sometimes do I get undefined?
function dataReverse(data) {
let x = { '11111111': 1, '00000000':2, '00001111':3, '10101010':4, '1': '11111111', '2': '00000000', '3': '00001111', '4': '10101010', }
let y = []
data = data.join('').match(/\d{8}/g)
data.forEach(a=> y.push(x[a]))
y = y.sort((a,b)=> b - a)
return y.map(a=> x[a]).join('').split('').map(a=> Number(a))
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
