From c171af84c1f23ded64822a83195cafb405678f96 Mon Sep 17 00:00:00 2001 From: angel Date: Mon, 27 Oct 2025 11:50:19 -0500 Subject: [PATCH] Upload files to "/" --- Untitled-1.ts | 91 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 Untitled-1.ts diff --git a/Untitled-1.ts b/Untitled-1.ts new file mode 100644 index 0000000..bf27dc3 --- /dev/null +++ b/Untitled-1.ts @@ -0,0 +1,91 @@ +// NOTE: YOUR ARE NOT ALLOWED TO USE EXISTING FUNCTION FROM +// THE JAVASCRIPT LIBRARY TO SORT ARRAYS. NOT CREDIT WILL BE +// GIVEN IF YOU DO + +// Function findSopt() takes two parameters an array of strings name list and +// a string named target. Array list contains strings storing country names sorted +// in ascending order and target contains the name of a country. Write code so +// that findSpot returns the location where targer fits alphabetically in the list +// of countries. +// For example, if list is ["Australia", "Brazil", "China, "Mexico", "United States"] +// and target is "Denmark", this funtion will return 3, because "Denmark" goes after China. +function findSpot(list: string[], target: string) { + // A helper function to remove non-alphabetic characters. + const stripNonAlpha = (str: string) => str.replace(/[^a-z]/gi, ''); + const strippedTarget = stripNonAlpha(target); + for (let i = 0; i < list.length; i++) { + const strippedItem = stripNonAlpha(list[i]!); + if (strippedTarget.localeCompare(strippedItem) < 0) { + return i; + } + } + return list.length; +} + + + +// Function mergeLists() takes two arrays of strings as parameters, list1 and list2. +// list1 is sorted inascending order and list2 is not sorted. +// Write code so that mergeSort places all elements from list2 into list1 leaving +// list1 sorted. If an element from list2 already exists in list1, it should not +// be added +// For example, if list1 is ["Australia", "Brazil", "China, "Mexico", "United States"] +// and list2 is ["France", "Brasil", "Canada"] +// this function will return +// ["Australia", "Brazil", "Canada", "China, "France", "Mexico", "United States"] +function mergeLists(list1: string[], list2: string[]) { + for (let i = 0; i < list2.length; i++) { + const element = list2[i] + let found = false; + for (let j = 0; j < list1.length; j++) { + if (list1[j] === element) { + found = true; + break; + } + } + if (!found) { + let spot = findSpot(list1, element!); + list1.splice(spot, 0, element!) + } + } + return list1 +} + +// Complete function displayArray sp that it returns arr string containing all +// the elements from array arr separated by commas. +// For example, if arr is ["Cameroon", "Albania", "Canada", "Barbados", "Zambia"] +// calling displayArray() should return +// Cameroon, Albania, Canada, Barbados, Zambia +function displayArray(arr: string[]){ + let s: string = ""; + for(let loc=0; loc