68 lines
2.0 KiB
TypeScript
68 lines
2.0 KiB
TypeScript
// humberto mundo
|
|
let animals: string[] = ["Zebra", "Koala", "Lion", "Elephant", "Eagle", "Cat", "Eagle"]
|
|
|
|
// Write a function named getLocation(list, word) to return the location of the parameter word
|
|
// in the array list, if it exist, returns -1 if it does not exists
|
|
|
|
function getLocation(list: string[], word: string): number {
|
|
return list.indexOf(word)
|
|
}
|
|
|
|
|
|
// Write a function named getLongest(list) which needs to return the longest string in array
|
|
// parameter array
|
|
|
|
function getLongest(array: string[] ): string {
|
|
if (!array) return "provide a array";
|
|
|
|
let long = array[0]; // init w/ first element
|
|
for (let i = 1; i < array.length; i++) {
|
|
// const element = list[i];
|
|
if (array[i]!.length > long!.length) { // this is why i love and hate typescript :3
|
|
long = array[i];
|
|
}
|
|
}
|
|
return long as string;
|
|
}
|
|
|
|
// extra cus y not
|
|
|
|
function getShortest(list: string[]): string {
|
|
if (!list) return "i need array >:("
|
|
|
|
let short = list[0]
|
|
|
|
for (let i = 1; i < list.length; i++) {
|
|
if (list[i]!.length < short!.length) {
|
|
short = list[i]
|
|
}
|
|
}
|
|
return short as string;
|
|
}
|
|
|
|
|
|
// Write a function named spellBackward(list, word) which it needs to return parameter word
|
|
// spell backwards if it exist in array list, otherwise it return word does not exist.
|
|
|
|
function spellBackward(list: string[], word: string): string {
|
|
if (!list.includes(word)) return "word does not exist";
|
|
return word.split("").reverse().join("");
|
|
}
|
|
|
|
|
|
//write the rest of the code here
|
|
|
|
console.log(getLocation(animals, "Eagle")) // uhhhh 4
|
|
console.log(getLocation(animals, "cat")) // should return -1 since its Cat not cat
|
|
|
|
console.log() // spacers
|
|
|
|
console.log(getLongest(animals)) // Elephant
|
|
console.log(getShortest(animals)) // Cat
|
|
|
|
console.log() // spacers
|
|
|
|
// i am the best programmer in the world
|
|
console.log(spellBackward(animals, "Eagle")) // should return elgaE
|
|
console.log(spellBackward(animals, "cat")) // should return "word does not exist"
|