From c5221640c3d15ea1e415e841363238246ebf170b Mon Sep 17 00:00:00 2001 From: afiqzudinhadi Date: Mon, 5 Aug 2024 22:26:53 +0800 Subject: [PATCH] feat: add logic --- index.js | 41 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 3099947..0229a72 100644 --- a/index.js +++ b/index.js @@ -1,3 +1,40 @@ -import { input } from '@inquirer/prompts'; +import { input } from "@inquirer/prompts"; -const answer = await input({ message: 'Enter your name' }); \ No newline at end of file +const answer = await input({ + message: + "Enter an array of string numbers containing only one letter separated by commas: (e.g. 932c, 832u32, 2344b)", +}); + +const array = answer.split(","); + +if (check_empty_array(array)) { + console.log([]); +} else { + if (check_only_one_letter(array)) console.log(sort_by_letter(array)); +} + +function check_empty_array(array) { + if (array[0] === "") { + return true; + } +} + +function check_only_one_letter(array) { + return array.every((element) => { + const letters = element.match(/[a-zA-Z]/g); + + if (letters.length !== 1) { + return console.log("Each string must contain exactly one letter."); + } + + return true; + }); +} + +function sort_by_letter(array) { + return array.sort((a, b) => { + const lettersA = a.match(/[a-zA-Z]/g); + const lettersB = b.match(/[a-zA-Z]/g); + return lettersA[0].localeCompare(lettersB[0]); + }); +}