Upload files to "/"
This commit is contained in:
91
Untitled-1.ts
Normal file
91
Untitled-1.ts
Normal file
@@ -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<arr.length; loc++){
|
||||
if(loc<arr.length-1)
|
||||
s = s+ arr[loc] +", "
|
||||
else
|
||||
s = s+ arr[loc]
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
///================ TASK ================
|
||||
// Write code to test your code with the following arrays
|
||||
|
||||
// ["Algeria", "Belgium", "Brazil", "Chile", "El Salvador", "Mexico", "Norway", "United States"]
|
||||
// ["Cameroon", "Albania", "Canada", "Barbados", "Zambia"]
|
||||
|
||||
// using the above arrays for list1 and list1 respectively, mergeLists should return
|
||||
// the following array:
|
||||
// ["Albania", "Algeria", "Barbados", "Belgium", "Brazil", "Cameroon", "Canada",
|
||||
// "Chile", "El Salvador", "Mexico", "Norway", "United States", "Zambia"]
|
||||
|
||||
let list1: string[] = ["Algeria", "Belgium", "Brazil", "Chile", "El Salvador", "Mexico", "Norway", "United States"]
|
||||
let list2: string[] = ["Cameroon", "Albania", "Canada", "Barbados", "Zambia"]
|
||||
|
||||
console.log("List 1")
|
||||
console.log(displayArray(list1))
|
||||
console.log()
|
||||
console.log("List 2")
|
||||
console.log(displayArray(list2))
|
||||
console.log()
|
||||
console.log("Combined List")
|
||||
console.log(displayArray(mergeLists(list1, list2)))
|
||||
Reference in New Issue
Block a user