JavaScript: NRZI encoding

Last update: 25 Jul 00:42
0
Students

NRZI code (Non-Return-to-Zero, Inverted) is one of the methods of linear coding. It has two signal levels and is used to transmit bit sequences containing only 0 and 1. NRZI is used, for example, in optical cables, where only two signal states are consistently recognized, light and dark.

Encoding principle

When transmitting a logical zero to the input of the coding device, the potential set at the previous clock cycle is transmitted (that is, the state of the potential does not change), and when transmitting a logical unit, the potential inverts.

nrzi

solution.js

Implement and export as default a function that accepts a string as a graphical representation of a linear signal and returns a binary code string. Carefully study the examples.

Examples

const signal1 = '_|¯|____|¯|__|¯¯¯';
nrzi(signal1); // '011000110100'

const signal2 = '|¯|___|¯¯¯¯¯|___|¯|_|¯';
nrzi(signal2); // '110010000100111'

const signal3 = '¯|___|¯¯¯¯¯|___|¯|_|¯';
nrzi(signal3); // '010010000100111'

const signal4 = '';
nrzi(signal4); // ''

const signal5 = '|';
nrzi(signal5); // ''

Tips

  • The | char indicates the signal switching and means that the signal level in the next clock cycle will be changed to the opposite compared to the previous one

For full access to the challenge you need a professional subscription.

A professional subscription will give you full access to all Hexlet courses, projects and lifetime access to the theory of lessons learned. You can cancel your subscription at any time.

Get access
130
courses
1000
exercises
2000+
hours of theory
3200
tests