83 lines
2.9 KiB
JavaScript
83 lines
2.9 KiB
JavaScript
|
|
// 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 = ["Zebra", "Koala", "Lion", "Elephant", "Eagle", "Cat", "Eagle"]
|
|
|
|
function circulate(list) {
|
|
if (list.length === 0 ) return []; // since if length of the arry is 0 return nothing
|
|
const lastElement = list.pop();
|
|
list.unshift(lastElement);
|
|
return list;
|
|
}
|
|
|
|
console.log(circulate(array1))
|
|
|
|
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 = ["Zebra", "Koala", "Lion", "Elephant", "Eagle", "Cat", "Eagle"];
|
|
|
|
function getHalf(list) {
|
|
if (list.length === 1) return list
|
|
// docs say Math.ceil() "returns the smallest integer greater than or equal to its number argument " in short terms rounds ups the number
|
|
return list.slice(0, Math.round(list.length / 2))
|
|
}
|
|
|
|
console.log(getHalf(array2))
|
|
|
|
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 = ["Lion", "Koala", "Lion", "Elephant", "Eagle", "Cat", "CAT"]
|
|
|
|
function removeDuplicates(list) {
|
|
let seen = new Set(); // create new collection
|
|
for (let i = 0; i < list.length; i++) {
|
|
let item = list[i]
|
|
// console.log(!seen.has(item))
|
|
if(!seen.has(item.toLowerCase())) {
|
|
seen.add(item.toLowerCase()) // add to new collection
|
|
list.splice(list.indexOf(item), list.indexOf(item))
|
|
// console.log(`added ${item.toLowerCase()}`)
|
|
}
|
|
}
|
|
return list
|
|
}
|
|
|
|
console.log(removeDuplicates(array3))
|
|
|
|
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 = ["Lion", "Cat", "Zebra", "Eagle", "Elephan", "Koala"]
|
|
|
|
function sortNames(list) {
|
|
return list.sort()
|
|
}
|
|
|
|
console.log(sortNames(array4))
|