Files
AP-Computer-Principles-Archive/typescriptisbetter.js
2025-10-27 11:49:53 -05:00

51 lines
1.9 KiB
JavaScript

// humberto mundo
var animals = ["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, word) {
return list.indexOf(word);
}
// Write a function named getLongest(list) which needs to return the longest string in array
// parameter array
function getLongest(array) {
if (!array)
return "provide a array";
var long = array[0]; // init w/ first element
for (var 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;
}
// extra cus y not
function getShortest(list) {
if (!list)
return "i need array >:(";
var short = list[0];
for (var i = 1; i < list.length; i++) {
if (list[i].length < short.length) {
short = list[i];
}
}
return short;
}
// 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, word) {
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"