diff --git a/StockBehavior.ts b/StockBehavior.ts new file mode 100644 index 0000000..8a0f734 --- /dev/null +++ b/StockBehavior.ts @@ -0,0 +1,27 @@ +//The array stocks stores the behavior of stocks for one year. +//The numbers in the array represent percentages. + +let stocks = [ 5, 6, 3, 4, 5, 8, 5, 3, 2, 10, 9, 5] + + +//A peak is when the percentage on the left of a given month M is less and the +//percentage on the right of month M is greater. For example, the list given above, contains +//three peaks, they are 6, 8, and 10 (positions 1, 5 and 9 respectively) +// Complete function getPeaks so that it returns an array containing all the locations +// of all peaks that a exists in the parameter array named list. +// If there are no peaks, this function should return an empty array ([]) +function getPeaks(list: number[]){ + let finishedarray: number[] = []; + for (let i = 0; i < list.length-1; i++) { + // console.log(`i is ${i}, arrindex is ${list[i]}, before, ${list[i-1]}, after ${list[i+1]}`) + if (list[i]! > list[i - 1]! && list[i]! > list[i+1]!) { + finishedarray.push(i); + } + } + return finishedarray; +} + +// for the array stocks shown above, getPeaks should return [1, 5, 9] +// WRITE CODE TO TEST YOUR FUNCTION HERE. + +console.log(getPeaks(stocks)) \ No newline at end of file diff --git a/StringChallenge.ts b/StringChallenge.ts new file mode 100644 index 0000000..29e274f --- /dev/null +++ b/StringChallenge.ts @@ -0,0 +1,15 @@ +function fizzBuzz(n: number): string[] { + let h = [] + for (let i=1; i<=n;i++) { + let string = '' + if (i % 3 === 0){ + string += 'Fizz' + } else if (i % 5 === 0) { + string += 'Buzz' + } else if (string === '') { + string += i + } + h.push(string) + } + return h +}; \ No newline at end of file diff --git a/problem2.ts b/problem2.ts new file mode 100644 index 0000000..e57cd0f --- /dev/null +++ b/problem2.ts @@ -0,0 +1,97 @@ +//Function displayList is complete +//Function displayList returns a string (a list of) of all test scores +//stored in the parameter array named scores +function displayList(scores: number[]): string +{ + let s = "" + for(let loc=0; loc 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"