Upload files to "/"
This commit is contained in:
82
LabArrays2.ts
Normal file
82
LabArrays2.ts
Normal file
@@ -0,0 +1,82 @@
|
||||
|
||||
// humberto mundo
|
||||
|
||||
|
||||
|
||||
// Objective: shift() and push()
|
||||
// Write a function named circulate(list). This function takes an array as parameter and
|
||||
// returns the same array but the elements move up one spot. For example, say the
|
||||
// array is ["Zebra", "Koala", "Lion", "Elephan", "Eagle", "Cat", "Eagle"], circulate will
|
||||
// return ["Eagle", "Zebra", "Koala", "Lion", "Elephant", "Eagle", "Cat"]
|
||||
let array1: string[] = ["Zebra", "Koala", "Lion", "Elephant", "Eagle", "Cat", "Eagle"]
|
||||
|
||||
function circulate(list: string[]) {
|
||||
if (list.length === 0 ) return []; // since if length of the arry is 0 return not
|
||||
const lastElement = list.pop()!;
|
||||
list.unshift(lastElement);
|
||||
return list;
|
||||
}
|
||||
|
||||
|
||||
|
||||
console.log()
|
||||
console.log()
|
||||
|
||||
// Objective: slice(1, 5)
|
||||
// Write a function named getHalf(). This function takes an array as a parameter and copies
|
||||
// the first half of an array and returns it. if the the array contains odd number of elements,
|
||||
// the middle element should belong to the first half.
|
||||
// For example ["Zebra", "Koala", "Lion", "Elephan", "Eagle", "Cat", "Eagle"], this function
|
||||
// return ["Zebra", "Koala", "Lion", "Elephant"]
|
||||
|
||||
let array2: string[] = ["Zebra", "Koala", "Lion", "Elephant", "Eagle", "Cat", "Eagle"];
|
||||
|
||||
function getHalf(list: string[]) {
|
||||
return list.slice(0, Math.ceil(list.length / 2))
|
||||
}
|
||||
|
||||
|
||||
|
||||
console.log()
|
||||
console.log()
|
||||
|
||||
// Write a function named removeDuplicates(). This function takes an array as a parameter, removes,
|
||||
// all duplicates and returns the new array (without duplicates)
|
||||
// For example ["Lion", "Koala", "Lion", "Elephan", "Eagle", "Cat", "CAT"], this function
|
||||
// return ["Lion", "Koala", "Elephant", "Eagle", "Cat"],
|
||||
|
||||
let array3: string[] = ["Lion", "Koala", "Lion", "Elephant", "Eagle", "Cat", "CAT"]
|
||||
|
||||
function removeDuplicates(list: string[]) {
|
||||
let seen = new Set<string>();
|
||||
// let newarray: string[] = [];
|
||||
for (let i = 0; i < list.length; i++) {
|
||||
let item: string = list[i]!
|
||||
// console.log(!seen.has(item))
|
||||
if(!seen.has(item.toLowerCase())) {
|
||||
seen.add(item.toLowerCase())
|
||||
list.splice(list.indexOf(item), list.indexOf(item))
|
||||
// console.log(`added ${item.toLowerCase()}`)
|
||||
}
|
||||
}
|
||||
return list
|
||||
}
|
||||
|
||||
console.log(removeDuplicates(array3))
|
||||
// want [ 'Lion', 'Koala', 'Elephant', 'Eagle', 'Cat' ]
|
||||
|
||||
console.log()
|
||||
console.log()
|
||||
// Write a function named sortNames(). This function takes an array as a parameter,
|
||||
// and returns a new array but with names sorted in ascending order
|
||||
// For example say list = ["Lion", "Cat", "Zebra", "Eagle", "Elephan", "Koala"], the
|
||||
// folowing call sortNames(list) will return
|
||||
// ["Cat", "Eagle", "Elephan", "Koala", "Lion", "Zebra"]
|
||||
|
||||
let array4: string[] = ["Lion", "Cat", "Zebra", "Eagle", "Elephan", "Koala"]
|
||||
|
||||
function sortNames(list: string[]) {
|
||||
return list.sort()
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user