Skip to content
Open
Show file tree
Hide file tree
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
9 changes: 3 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"devDependencies": {
"@types/chai": "^4.3.4",
"@types/mocha": "^10.0.1",
"@types/node": "^22.7.5",
"chai": "^4.3.7",
"mocha": "^10.2.0",
"ts-node": "^10.9.2",
Expand All @@ -20,4 +21,4 @@
"description": "Simple coding challenge for Evie!",
"main": "dist/index.js",
"author": "Evie Networks"
}
}
26 changes: 26 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,27 @@
// The CLI entry point should live here!
import { reverseSentence } from './reverse';

// This line is used to receive the arguments from the command line + skipping 'node' and the script path
const args = process.argv.slice(2);

// The first argument is the sentence and any other arguments are options.
const sentence = args[0] || '';

// Check if the user wants to reverse words or letters
const reverseWords = args.includes('--word');
const reverseLetters = args.includes('--letter');

// Try to reverse sentence based on the options and catch if theres any error
try {
const result = reverseSentence(sentence, reverseWords, reverseLetters);
console.log(result);
} catch (error) {
//Narrow down to handle errors as typescript error
if (error instanceof Error) {
console.error(error.message);
} else {
console.error("An unknown error occurred.");
}
}


28 changes: 27 additions & 1 deletion src/reverse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,31 @@ export const reverseSentence = (
reverseWords: boolean,
reverseLetters: boolean
): string => {
throw new Error("Not implemented yet!");
try {
// Split the provided sentence into an array as soon as space found.
let words = sentence.split(' ');

// This reverse the order of the words
if (reverseWords) {
words.reverse();
}

// This reverse the letters in each words
if (reverseLetters) {
words = words.map(word => word.split('').reverse().join(''));
}

// Join all the words back together and return the result.
return words.join(' ');
} catch (error) {
//Narrow down to handle errors as typescript error
if (error instanceof Error) {
console.error('An error occurred:', error.message);
throw new Error(error.message || 'Failed to reverse sentence.');
} else {
console.error('An unexpected error occurred:', error);
throw new Error('Failed to reverse sentence due to an unknown error.');
}
}
};