Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Hello World Script #72

Merged
merged 1 commit into from
Oct 8, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions Javascript/elloWorld.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Usage: $ node HelloRube/Javascript/elloWorld.js Hello World

let input = "";
process.argv.forEach((val, index, array) => {
if ([0, 1].indexOf(index) === -1) {
if (index !== 2) {
val = ` ${val}`;
}
input += val;
}
});

const ALPHABET = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ".split(
""
);

const transformTextCharacterToAlphabetIndex = array => {
return new Promise((resolve, reject) => {
const indexArray = array.map(character => {
const index = ALPHABET.indexOf(character);
if (index !== -1) {
return index;
}
});
resolve(indexArray);
});
};

const transformAlphabetIndexToCharacterArray = array => {
return new Promise((resolve, reject) => {
const characterArray = array.map(index => {
return ALPHABET[index];
});
resolve(characterArray);
});
};

const convertToArray = text => {
return new Promise((resolve, reject) => {
resolve(text.split(""));
});
};

const convertToString = text => {
return new Promise((resolve, reject) => {
resolve(text.join(""));
});
};

class HelloWorld {
constructor(text) {
this.text = text;
}

async transform(initialText) {
let promises = [
convertToArray,
transformTextCharacterToAlphabetIndex,
transformAlphabetIndexToCharacterArray,
convertToString
];
let lastResult = initialText;
for (let i = 0; i < promises.length; i++) {
lastResult = await promises[i](lastResult);
}
return lastResult;
}

say() {
if (this.text === "" || this.text === undefined) {
console.log("You forgot to enter some text...");
return;
}
this.transform(this.text)
.then(text => console.log(text))
.catch(error => console.log(error));
}
}
new HelloWorld(input).say();