feat: add logic

input string & target, turn into array object, sort array, output message
This commit is contained in:
afiqzudinhadi 2024-07-04 22:56:00 +08:00
parent 3029c98cd0
commit d45a1c229e
4 changed files with 47 additions and 198 deletions

View file

@ -1,3 +1,12 @@
import { input } from '@inquirer/prompts';
const answer = await input({ message: 'Enter your name' });
const answer = await input({ message: 'Enter an array of numbers separated by commas:' });
const array = answer.split(',').filter(Boolean).map(Number).sort((a, b) => a - b);
const target = await input({ message: 'Enter a target number:' });
const index = array.findIndex((num) => num == target);
console.log('Sorted array:', array);
console.log(index === -1 ? 'Target number not found' : `Target number found at index ${index}`);