// 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