Skip to content

Instantly share code, notes, and snippets.

@mauroquinteros
Created July 14, 2022 17:11
Show Gist options
  • Save mauroquinteros/92999d88e28eaeaf5d04355bac5e960f to your computer and use it in GitHub Desktop.
Save mauroquinteros/92999d88e28eaeaf5d04355bac5e960f to your computer and use it in GitHub Desktop.
Read values from console input in Node
const readline = require("readline");
const { Readable } = require("stream");
const readableStream = new Readable();
readableStream.push("3 abb aab ababa");
readableStream.push(null);
const input = readline.createInterface({
input: readableStream,
output: process.stdout,
terminal: false,
});
let inputList = [];
input.on("line", (value) => {
console.log("Input: ", value);
inputList = value.split(" ");
});
input.on("close", () => {
const values = getValues(inputList);
const goodValues = transformValues(values);
console.log("Output: ", goodValues);
});
function getValues(inputList) {
const values = [];
inputList.forEach((value, index) => {
if (index != 0) {
values.push(value);
}
});
return values;
}
function transformValues(values) {
const goodValues = [];
values.forEach((value) => {
let characters = value.split("");
let newCharacters = [];
characters.forEach((letter, index) => {
if (index == 0) {
newCharacters.push(letter);
}
if (letter != characters[index - 1] && index != 0) {
newCharacters.push(letter);
}
});
goodValues.push(newCharacters.join(""));
});
return goodValues.join(" ");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment